# ActiveRecord query, dot notation for associations

**URL:** <https://www.ruby-forum.com/t/activerecord-query-dot-notation-for-associations/83691>\
**Category:** Rails\
**Created:** [January 28, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/activerecord-query-dot-notation-for-associations/83691 "2007-01-28T22:14:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![merg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@merg](https://www.ruby-forum.com/u/merg)\
**Post date:** [January 28, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/activerecord-query-dot-notation-for-associations/83691/1 "2007-01-28T22:14:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![merg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@merg](https://www.ruby-forum.com/u/merg)\
**Post date:** [January 29, 2007, 3:42am UTC](https://www.ruby-forum.com/t/activerecord-query-dot-notation-for-associations/83691/2 "2007-01-29T03:42:47Z")

</div>

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](http://www.elliottblatt.com)

---

<div class="post-metadata">

**Author:** ![merg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@merg](https://www.ruby-forum.com/u/merg)\
**Post date:** [January 30, 2007, 11:51am UTC](https://www.ruby-forum.com/t/activerecord-query-dot-notation-for-associations/83691/3 "2007-01-30T11:51:48Z")

</div>

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 B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
