# Backward associations

**URL:** <https://www.ruby-forum.com/t/backward-associations/143569>\
**Category:** Rails\
**Created:** [July 20, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/backward-associations/143569 "2008-07-20T17:12:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [July 20, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/backward-associations/143569/1 "2008-07-20T17:12:43Z")

</div>

hi everyone,  
my situation is describes as:

User has\_many Blog  
Blog has\_many BlogPost

BlogPost belongs\_to Blog  
Blog belongs\_to User

now, to “bridge” User with Post I wrote in User model:

has\_many :blog\_posts, :through =\> :blogs, :source =\> :posts

and so far it works.

to bridge BlogPost with User (author) I wrote in BlogPost model:  
delegate :user, :to =\> :blog

the problem is, since there’s no belongs\_to :through, I need to find a  
better AR compliant way to bridge BlogPost with User, since I have to  
use a counter\_cache field on User model that contains the number of  
BlogPost he made.

of course I can write my callbacks to handle this situation, but I  
feel like reinventing the wheel.

somebody got some tips to share?

thanks

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [July 20, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/backward-associations/143569/2 "2008-07-20T17:14:30Z")

</div>

On 20 Jul 2008, at 16:11, Claudio P. wrote:

> now, to “bridge” User with Post I wrote in User model:  
> use a counter\_cache field on User model that contains the number of  
> BlogPost he made.

Have you tried has\_one :through ?

Fred

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [July 20, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/backward-associations/143569/3 "2008-07-20T17:15:41Z")

</div>

I forgot to add that yes, I may duplicated the user\_id field in  
BlogPost, but I’m not sure it’s the better way since there’s already  
Blog that contains such information.

Would make sense however if there are many users that can write  
BlogPost in another person’s Blog, but this is not the case.

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [July 20, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/backward-associations/143569/4 "2008-07-20T17:18:36Z")

</div>

hi Frederick,  
yes, I’ve tried some hours ago, but it failed, I’ve tried again now  
and it seems to work, maybe there was an error somewhere.

thanks.

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [July 20, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/backward-associations/143569/5 "2008-07-20T17:21:32Z")

</div>

oh uhm… I’m messing up things, sorry for that (fast experimenting),  
no, it doesn’t work:

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column  
‘blogs.blog\_id’ in ‘where clause’: SELECT `users`.\* FROM `users`  
INNER JOIN blogs ON users.id = blogs.user\_id WHERE  
((`blogs`.blog\_id = 1))
