Forum: Ruby on Rails ActiveRecord query, dot notation for associations

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.
Ee238d259f37429f7f55bd53102554cd?d=identicon&s=25 merg (Guest)
on 2007-01-28 22:14
(Received via mailing list)
Am I missing something in Active record, or is there a plug-in
available which easily allows to use the 'dot-notation' in deeply
linked (that is, more then one link away) associations. Suppose I want
to count the posts in a blog of a certain user, can I write something
like :condition :post.blog.user.name = ?

Merg
419c9cc258895bed66c89892721c2971?d=identicon&s=25 Elliott Blatt (eblatt)
on 2007-01-29 03:42
Merg:

I think that you might want to try this this.  I don't know if this will
work or not, but this is what I would experiment with:
In your post model define a new method called 'comments_by_user'
#---------------
#post.rb
class post < ActiveRecord::Base
  has many :comments
  belongs_to :user

  def comments_by_user(user)
    comments = Comment.find(:conditions => ["user_id = ?  and post_id =
?",
                                                    user.id, self.id])
   end

end

#----
Invoke like this in your views:

<%= post.comments_by_user(user).count-%>

Lemme know if it works for you.


-Elliott
www.elliottblatt.com
Ee238d259f37429f7f55bd53102554cd?d=identicon&s=25 merg (Guest)
on 2007-01-30 11:51
(Received via mailing list)
Hi Andreas, this helps!!!!

I'm used to EJBQL and HQL wherel the dot-notation is common.

regards,
Peter

On Jan 29, 3:42 am, Elliott Blatt <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.