I have tow tables:
User:
user_id
user_blogs:
user_id | blog_id
blogs:
blog_id | source | identifier
Comments:
source | identifier | Field3
I want to be able to select all comments in the blogs that a user owns.
My models are related:
class User < ActiveRecord::Base
has_many :user_blogs
has_many :blogs, trhough: :user_blogs
end
class blogs < ActiveRecord::Base
has_many :comments,
:foreign_key => :source,
:primary_key => :source,
:conditions => Proc.new {
{:identifier=> self.identifier}
}
end
Right now I can retrieve all user comments using this:
User.first.blogs.map{|b| b.comments}
But this creates one query for each blog.
Is there a way to do this in one single step?