Reputation: 3235
I have a Rails4 model (my_model
) which may or may not have an attribute. I don't want to save the attribute on my db after I call:
my_model.save
or
my_model.create
but I would like to have access to the value of this possible attribute (or nil if it doesn't exist) after doing:
my_model.new(attribute: possible_attribute)
Is there a way to achieve this result?
Upvotes: 0
Views: 103
Reputation: 29369
Define the possible_attribut
e as attr_accessor
and attr_accessibl
e
class MyModel
attr_accessor :possible_attribute
attr_accessible :possible_attribute
end
Now you can do
m = MyModel.new(:possible_attribute => "value")
m.possible_attribute #value
And
m.save
will not save possible_attribute
Upvotes: 1