Combined Fields in Rails

What is the best way to solve the problem? User inputs and editing the line "Tom had a dog." I would like the model and database to store "tom" and "have a dog" as separate fields. I remember the solution to this problem when working with phone numbers, but I can’t remember how I did it.

+3
source share
1 answer

You can create a setter in your model that is not tied to a database field ... this setter will contain logic that determines how the string is divided into two parts, and sets two fields accordingly:

class MyModel

  def mysetter=(string)
      # your logic to split up the string
      field1 = ...
      field2 = ...
  end
end
+6
source

Source: https://habr.com/ru/post/1705873/


All Articles