On Tue, May 11, 2010 at 8:43 AM, David Zhu <[email protected]> wrote:
> On May 11, 8:36 am, Colin Law <[email protected]> wrote:

>> I believe that if you also say Page has_many davids through comments
>> then you can get at all the davids for a post by
>> @post.davids
>> and hence you can use @posts.davids.count
> i need a join table? is the join table my comments table?

Yes but..

First I notice that you continue to use code of the form

   Post.comments. ....

Which won't work, since the associations like belongs_to, has_many ...
create INSTANCE rather than class methods.

You need to use something like

  some_post.comments. ..

where some_post refers to a particular instance of Post.

Second,  I suspect that David here is a hypothetical model class, and
that what you might really be trying to do is count all the comments
made by a particular user.  Maybe you want something like:

class Post < ActiveRecord::Base
    has_many :comments
end

class User < ActiveRecord::Base
end

class Comment < ActiveRecord::Base
   belongs_to :post
   belongs_to : user
   named_scope :by_user, lambda {|user| :conditions => {:user => user}
end

then

 some_post = Post.find(params[:id])  # or some other code to get a
particular post
 david = User.find_by_userid("david")  # or some other code to get a
particular user

 some_post.comments.by_user(david).count



-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to