Consider a model without an initialize method, defined as follows:
class User < ActiveRecord::Base
end
It has 2 properties: name & age.
It is possible to create a new object by calling User.new('Joe Brown', 21)? Is there another "shorthand" way of creating the object?
Or do you have to define an initialize method in order to do this in one line?
With ActiveRecord:
User.new(:name => "Joe Brown", :age => 21)
You can also specify the fields :
User.new :name => "Joe Brown", :age => 21
or
User.new name: "Joe Brown", age: 21
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With