0
我有一个Rails4模型(my_model
),它可能有也可能没有属性。我不想保存属性上我的数据库后,我打电话:Rails4:模型中的可选参数
my_model.save
或
my_model.create
,但我想有机会获得这一可能的属性值(或零,如果它不存在),后做事:
my_model.new(attribute: possible_attribute)
为T这里有一种方法来实现这个结果?
我有一个Rails4模型(my_model
),它可能有也可能没有属性。我不想保存属性上我的数据库后,我打电话:Rails4:模型中的可选参数
my_model.save
或
my_model.create
,但我想有机会获得这一可能的属性值(或零,如果它不存在),后做事:
my_model.new(attribute: possible_attribute)
为T这里有一种方法来实现这个结果?
定义possible_attribut
Ë为attr_accessor
和attr_accessibl
Ë
class MyModel
attr_accessor :possible_attribute
attr_accessible :possible_attribute
end
现在你可以做
m = MyModel.new(:possible_attribute => "value")
m.possible_attribute #value
而且
m.save
不会救possible_attribute