Ruby on rails 更新属性不';我不能扮演一个角色

Ruby on rails 更新属性不';我不能扮演一个角色,ruby-on-rails,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3.1,我有一个模型,有两条属性线 attr_accessible ...., :as => :user attr_accessible ...., :as => :admin 然后在我的控制器里我做这个 if @user.update_attributes(params[:user],:as => :user) 但是,对于1,我得到了错误数量的参数2:@user.assign_atributes(params[:user],:as=>:user)有效 我用的是mongoid。有

我有一个模型,有两条属性线

attr_accessible ...., :as => :user
attr_accessible ...., :as => :admin 
然后在我的控制器里我做这个

if @user.update_attributes(params[:user],:as => :user)
但是,对于1,我得到了错误数量的参数2:
@user.assign_atributes(params[:user],:as=>:user)
有效


我用的是mongoid。有什么想法吗?

我想你在你的Mongoid版本中发现了一些尚未实现的东西——但是请检查一下更高版本的Mongoid,它似乎是从2.2.1开始实现的

文档说:as是Mongoid::Document中的一个有效选项

(见本页底部)

但是它说:通过将角色作为构造函数或创建方法的选项提供,可以按角色确定质量分配的范围

它没有特别提到更新属性-更新属性不是构造函数

检入源代码后发现,在Mongoid版本<2.2.1的update_attributes()中没有实现它,但在更高版本中实现了它

如果您使用的是更高版本的Mongoid,但仍然遇到问题, 我建议将此作为bug发布在Google Mongoid组上,并提及您的Mongoid版本号

另见:


编辑:

看起来它是Mongoid 2.1.7中缺少的功能

在mongoid源代码中,update_attributes()调用write_attributes(),它不知道选项:as

# Allows you to set all the attributes for a particular mass-assignment security role                                                                                                                                                                                                                                   
# by passing in a hash of attributes with keys matching the attribute names                                                                                                                                                                                                                                             
# (which again matches the column names)  and the role name using the :as option.                                                                                                                                                                                                                                       
# To bypass mass-assignment security you can use the :without_protection => true option.                                                                                                                                                                                                                                
#                                                                                                                                                                                                                                                                                                                       
# @example Assign the attributes.                                                                                                                                                                                                                                                                                       
#   person.assign_attributes(:title => "Mr.")                                                                                                                                                                                                                                                                           
#                                                                                                                                                                                                                                                                                                                       
# @example Assign the attributes (with a role).                                                                                                                                                                                                                                                                         
#   person.assign_attributes({ :title => "Mr." }, :as => :admin)                                                                                                                                                                                                                                                        
#                                                                                                                                                                                                                                                                                                                       
# @param [ Hash ] attrs The new attributes to set.                                                                                                                                                                                                                                                                      
# @param [ Hash ] options Supported options: :without_protection, :as                                                                                                                                                                                                                                                   
#                                                                                                                                                                                                                                                                                                                       
# @since 2.2.1                                                                                                                                                                                                                                                                                                          
def assign_attributes(attrs = nil, options = {})
  _assigning do
    process(attrs, options[:as] || :default, !options[:without_protection]) do |document|
      document.identify if new? && id.blank?
    end
  end
end
但是如果您查看Mongoid 2.3.1的源代码,您会发现它是从2.2.1开始在那里实现的

write_attributes()调用assign_attributes,该选项支持:as

# Allows you to set all the attributes for a particular mass-assignment security role                                                                                                                                                                                                                                   
# by passing in a hash of attributes with keys matching the attribute names                                                                                                                                                                                                                                             
# (which again matches the column names)  and the role name using the :as option.                                                                                                                                                                                                                                       
# To bypass mass-assignment security you can use the :without_protection => true option.                                                                                                                                                                                                                                
#                                                                                                                                                                                                                                                                                                                       
# @example Assign the attributes.                                                                                                                                                                                                                                                                                       
#   person.assign_attributes(:title => "Mr.")                                                                                                                                                                                                                                                                           
#                                                                                                                                                                                                                                                                                                                       
# @example Assign the attributes (with a role).                                                                                                                                                                                                                                                                         
#   person.assign_attributes({ :title => "Mr." }, :as => :admin)                                                                                                                                                                                                                                                        
#                                                                                                                                                                                                                                                                                                                       
# @param [ Hash ] attrs The new attributes to set.                                                                                                                                                                                                                                                                      
# @param [ Hash ] options Supported options: :without_protection, :as                                                                                                                                                                                                                                                   
#                                                                                                                                                                                                                                                                                                                       
# @since 2.2.1                                                                                                                                                                                                                                                                                                          
def assign_attributes(attrs = nil, options = {})
  _assigning do
    process(attrs, options[:as] || :default, !options[:without_protection]) do |document|
      document.identify if new? && id.blank?
    end
  end
end
您可以通过以下方式找到源代码:

$ find   ~/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.1.7/lib/ -type f -exec grep -l 'def write_attributes' {} \;
~/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.1.7/lib/mongoid/attributes.rb
$ emacs ~/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.1.7/lib/mongoid/attributes.rb

你用的是哪个版本的mongoid?哇,谢谢你的研究。我有2.3.1版,但它仍然不工作。我将提交它作为一个错误。我想他们有代码,但是update_attributes函数只需要一个参数。