将属性传递给Mongoid update_attributes()

时间:2011-11-07 02:01:01

标签: ruby-on-rails ruby mongoid

我想从mongoid中使用这个函数:

person.update_attributes(first_name: "Jean", last_name: "Zorg")

但我想传递另一个变量的所有属性。我该怎么做?

编辑:感谢大家的回复。我是红宝石的新手,所以起初我以为我犯了一个愚蠢的错误。错误是在一个完全不同的地方,正确的代码,为了您的享受:

  def twitter
    # Scenarios:
    # 1. Player is already signed in with his fb account:
    #    we link the accounts and update the information.
    # 2. Player is new: we create the account.
    # 3. Player is old: we update the player's information.
    # login with a safe write.
    puts "twitter"
    twitter_details = {
      twitter_name: env["omniauth.auth"]['user_info']['name'], 
      twitter_nick: env["omniauth.auth"]['user_info']['nickname'],
      twitter_uid: env["omniauth.auth"]['uid']
    }
    if player_signed_in?
      @player = Player.find(current_player['_id']) 
    else
      @player = Player.first(conditions: {twitter_uid: env['omniauth.auth']['uid']})
    end

    if @player.nil?
      @player = Player.create!(twitter_details)
    else
      @player.update_attributes(twitter_details)
    end

    flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Twitter"
    sign_in_and_redirect @player, :event => :authentication
  end

3 个答案:

答案 0 :(得分:6)

update_attributes方法接受一个Hash参数,因此如果你有一个Hash h,只有:first_name:last_name个键,那么:

person.update_attributes(h)

如果您的Hash有更多密钥,那么您可以使用slice来提取您想要的密钥:

person.update_attributes(h.slice(:first_name, :last_name))

答案 1 :(得分:5)

如果查看Mongoid的源代码,您会在文件中看到update_attributes的定义 .rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.3.1/lib/mongoid/persistence.rb

# Update the document attributes in the datbase.                                                                                                         
#                                                                                                                                                        
# @example Update the document's attributes                                                                                                              
#   document.update_attributes(:title => "Sir")                                                                                                          
#                                                                                                                                                        
# @param [ Hash ] attributes The attributes to update.                                                                                                   
#                                                                                                                                                        
# @return [ true, false ] True if validation passed, false if not.                                                                                       
def update_attributes(attributes = {})
  write_attributes(attributes); save
end

它需要哈希 - 这意味着你可以使用哈希作为传入的变量。 e.g。

my_attrs = {first_name: "Jean", last_name: "Zorg"}

person.update_attributes( my_attrs )

答案 2 :(得分:2)

update_attributes方法中发生了什么,实际上,在Rails平台上,变量会在必要时在内部放入哈希值。

以下是等同的:

person.update_attributes(first_name: "Jean", last_name: "Zorg")
person.update_attributes({first_name: "Jean", last_name: "Zorg"})
person.update_attributes(name_hash)

name_hash是:

name_hash = {first_name: "Jean", last_name: "Zorg"}