阅读有关批量分配的信息
您可以创建角色
attr_accessible :first_name, :last_name # default role
attr_accessible :first_name, :last_name, :premium, :as => :special # 'special' role
调用.update_attributes(params)
此模型时,将使用默认角色;如果:premium
在 中找到params
,则会抛出错误。
在POST
您的特殊形式的方法中,您可以为like指定:special
角色,指示使用允许大规模分配属性的角色。update_attributes
.update_attributes(params, :special)
update_attributes
:special
:premium
您可以根据 in 的某些属性有条件地传递角色名称params
,例如具有您提到的值的电话号码
@the_model.update_attributes(params, params[:phone_no].present? ? :special : :default)
如果这些条件更复杂,您可以考虑将它们作为类方法添加到模型中
def self.special_role?(params)
# fancy conditions here, returning true/false
end
然后update_attributes
可能看起来像
@the_model.update_attributes(params, TheModel.special_role?(params) ? :special : :default)