views:

10

answers:

2

Hi

Let's say I have two models like so:

class Comment < ActiveRecord::Base
  belongs_to :user
  named_scope :about_x :conditions => "comments.text like '%x%')"
end

class User < ActiveRecord::Base
  has_many :comments
end

I would like to use the models so that I can return all the users and all comments with text like '%x%'

all_user_comments_about_x = User.comments.about_x

How to proceed?

Thank you

A: 

If I understood correctly, you need

# all comments about x and the user who did it
@comments = Comment.about_x(:include => :user)

or

@user = User.first
# all @user's comments about x
@comments = @user.comments.about_x
j.
thakns j, if I wanted to add a named_scope to the user model that used the about_x named scope of the comment model is this possible?
mustafi
A: 

Try Following

class User < ActiveRecord::Base
  has_many :comments
  named_scope :about_x, :joins => :comments, :conditions => ["comments.text like '%x%'"]
end

and then

@comments = User.about_x
Salil