Understanding Ruby on Rails ActiveRecord model Accessors -
My model is a bunch of fields in "datafile", which I want to set from outside the model, for example
file = DataFile.new file.owner = 123
Now, as far as I know, I will have to place "attr_accessor: field" in my model for every model However, I want to modify from the outside, however, the above code runs fine without defining any atre_accessors, setting the Owner field to 123 Is. Why is it like this?
I was expecting a "no define method" error or something like that. /p>
Because the Rail 'ORM uses ActiveRecord patterns, two methods are automatically associated with each database The table that is created for the column: column name , and column name = . This is "automatically" as a result of your model from ActiveRecord :: Base. These methods are defined using Ruby's Metaprogramming features and are dynamically created at the time of class creation.
For more information about what is actually going on, I will take a look at the railway source. However, the above is probably enough to understand what is happening to you.
Comments
Post a Comment