Arnold Roa
Arnold Roa

Reputation: 7718

Twitter Gem and Rails issue saving user

im building an application that extracts all mentions using twitter, i have a Profile model where i want to save all the users that sent a mention. On that table i have twitter_id field where i want to store the id retrieved through the twitter API.. and other fields like description, screen_name etc that have the same names.

  # a.tc is a Twitter object already authenticated
  tws = a.tc.mentions({:count => 200})

  # foreach mention
  tws.each do |t|
    # Check if we already have it saved
    p = Profile.find_by_twitter_id t.user.id
    if p.nil?
      # Profile doesnt exist, try to save it
      p = Profile.new(t.user.to_hash) # ERROR!
      p.twitter_id = t.user.id
      p.save
    end

I already tried many things but everything trows an error... im a ruby noob =P

Upvotes: 1

Views: 189

Answers (1)

Simon Woker
Simon Woker

Reputation: 5034

You need to either delete the ID or assign only the attributes that are available in Profile:

  usr = t.user.to_hash
  usr.delete :id # or "id", I'm not sure how the Hash looks like exactly
  ## delete all the other keys that are not neccessary
  p = Profile.new usr

or use this way. This is the better way because you cannot assign attributes by accident

 p = Profile.new (:screen_name => t.user.screen_name, ... and so on... )

Upvotes: 2

Related Questions