Forum: Ruby on Rails need help converting SQL query to find or AR association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fd4ee9ae435369ab446489607701f518?d=identicon&s=25 vince (Guest)
on 2009-03-30 04:51
(Received via mailing list)
I have 3 models

comments(user_id, title)
followings(user_id, follower_id)
users(email)

I need to find all comments made by people that a user is following,
which in effect is this simple SQL query:

select comments.title
from comments, followings, users
where users.id = followings.follower_id
  and followings.user_id = comments.user_id
  and users.id = 1

Can someone help?  I tried this but it didn't work:

class User ...
  has_many :followings

class Following ...
  has_many :comments, :foreign_key => 'user_id'

script/console
User.find(:first).followings    (this works)
Following.find(:first).comments   (this works)
User.find(:first).followings.comments   (this does NOT work)
8f72e436a3107703e6ee950512036598?d=identicon&s=25 shenry (Guest)
on 2009-03-30 06:19
(Received via mailing list)
You might want something like:

class Comment < ActiveRecord::Base
  belongs_to :user
end

class Following < ActiveRecord::Base
  belongs_to :follower, :class_name => 'User', :foreign_key =>
'follower_id'
  belongs_to :followed, :class_name => 'User', :foreign_key =>
'user_id'
end

class User < ActiveRecord::Base
  has_many :comments
  has_many :admirers, :class_name => 'Following', :foreign_key =>
'user_id'
  has_many :followed, :class_name => 'Following', :foreign_key =>
'follower_id'
end

u = User.first

Then to get an array of all comments made by other users that this
user is following:

u.followings.map { |f| f.followed.comments }.flatten

There's probably a more elegant way to do this (and I'm sure you could
improve on my choice of words for 'admirers' and 'followed' :) ) but
hopefully it gets you in the right direction.

Stu
Fd4ee9ae435369ab446489607701f518?d=identicon&s=25 vince (Guest)
on 2009-03-30 07:54
(Received via mailing list)
Thanks Stu.  The solution above works but I'm looking for a find
statement that will perform a single db query (like the SQL statement
I posted) versus performing multiple queries and adding them using
ruby.  If a user has a hundred followers wouldn't your solution
perform a hundred queries?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-30 10:14
(Received via mailing list)
On Mar 30, 6:53 am, vince <stha...@gmail.com> wrote:
> Thanks Stu.  The solution above works but I'm looking for a find
> statement that will perform a single db query (like the SQL statement
> I posted) versus performing multiple queries and adding them using
> ruby.  If a user has a hundred followers wouldn't your solution
> perform a hundred queries?
>
Actually more like 200 :=)

One way is to just use the same joins that your raw sql had, just in a
more activerecordy way eg

Comment.find :all, :joins => {:user => :followings}, :conditions =>
["followings.follower_id = ?", foo]

select * from comments
inner join users on users.id = comments.user_id
inner join followings on followings.user_id = users.id
where followings.follower_id = 1

(I may have got the order on your self referential join table back to
front, but should be easy enough to fix if that is the case)

Fred
This topic is locked and can not be replied to.