ruby on rails - How to temporarily change the default value of column? -
I have a model that has several predefined rows (> 100) and can be edited by the user as well. I want to distinguish between pre-defined and user-added rows.
Because the database will be edited by a third-party application, I also want to be in the safe side and set the default value in the database schema predefined
to False
, because setting it to true will take serious restrictions on the line (i.e. it can never be removed)
On the other hand, the installed script creates 100 predefined rows; For each line Predefined = true
must specify who is going to hide the script.
This is not bad, but if there is an easy way to change the train by default, then it makes my script more friendly.
In other words: I want to write it:
MyModel.create (: data = & gt; "value", predefined = true)
is it possible?
Edit: This is just an examply, actually there are some more columns I have to set aside for the predefined column.
You can do this in two different ways:
Your migration In:
t column: boolean: string,: default => True
This defines the default value directly in the database when you change it, you have to create a new migration.
In your model:
def before_create predefined = true if predefined. Nil? End
defines this value as "true" unless you define it in advance. You can change the value by turning it into a model.
Comments
Post a Comment