开发商!我无法理解接下来的情况
例如我有模型
class Pg::City < ActiveRecord::Base
belongs_to :country
#virtual accessors
attr_accessor :population
#attr_accessible :city, :isdisabled, :country_id
end
我可以使用这样的代码:
c = Pg::City.new({:population=>1000})
puts c.population
1000
但是如果我取消注释上面的 attr_accessible 代码,则会抛出警告
WARNING: Can't mass-assign protected attributes: population
如何将虚拟属性与模型属性一起用于批量分配?
谢谢!
Using attr_accessor
添加变量不会自动将其添加到attr_accessible
。如果你要使用attr_accessible
,那么你需要添加:population
到列表:
attr_accessor :population
attr_accessible :city, :isdisabled, :country_id, :population
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)