CamelCamelCamel
CamelCamelCamel

Reputation: 5200

Passing attributes to Mongoid update_attributes()

I want to use this function from mongoid:

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

But I want to pass in all the attributes from another variable. How do I do that?

Edit: Thanks everyone for your reply. I'm new to ruby so at first I thought I just made a silly mistake with this. The bug was in a completely different place, the correct code, for your enjoyment:

  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

Upvotes: 0

Views: 4649

Answers (3)

mu is too short
mu is too short

Reputation: 434945

The update_attributes method takes a Hash argument so if you have a Hash, h, with just :first_name and :last_name keys then:

person.update_attributes(h)

If your Hash has more keys then you can use slice to pull out just the ones you want:

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

Upvotes: 6

WattsInABox
WattsInABox

Reputation: 4636

What's happening in the update_attributes method and, indeed, across the Rails platform is variables get put into a hash internally, when necessary.

So the following are equivalent:

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

Where name_hash is:

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

Upvotes: 2

Tilo
Tilo

Reputation: 33752

if you look at the source code of Mongoid, you'll see the definition of update_attributes in the file .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

It takes a Hash -- that means you can use a Hash as the variable that's passed in. e.g.

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

person.update_attributes( my_attrs )

Upvotes: 5

Related Questions