Jmlevick
Jmlevick

Reputation: 6686

How to Tweet from multiple acounts with twitter Gem in Rails?

I have an application wich has Oauth access using Twitter as provider. I also have the ability to ask the logged user permisson to Read and Write in his/her account and once a user authorized the app, I can send tweets as the user with something like:

u = User.find(id)
u.twitter.update("Some-Status-Here")

in the rails console... What I want to do is to Tweet as all the users in one command, but if I try something like:

u = User.all
u.twitter.update("Some-Status-Here")

I get this error:

undefined method `twitter' for #<Array:0x00000002e2f188>

How can I tweet as all the users in one command? What am I doing wrong? I feel it is a very basic thing I'm missing... Can someone help me?

Thank You.

Upvotes: 1

Views: 151

Answers (1)

ryanb
ryanb

Reputation: 16287

The User.all method returns an array of users, not a single user. If you want to call a method on each of the users you need to loop through it.

users = User.all
users.each do |user|
  user.twitter.update("Some status here")
end

That said, I don't recommend updating a user's Twitter status without first showing them the message and asking their consent.

Also, there are likely more efficient ways to do this than the above, so keep an eye on performance and the API hit limit.

Upvotes: 2

Related Questions