# Ruby on Rails Associations - Practical Applications

**URL:** <https://www.ruby-forum.com/t/ruby-on-rails-associations-practical-applications/188327>\
**Category:** Rails\
**Created:** [May 10, 2010, 10:57am UTC](https://www.ruby-forum.com/t/ruby-on-rails-associations-practical-applications/188327 "2010-05-10T10:57:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cam](https://www.ruby-forum.com/u/cam)\
**Post date:** [May 10, 2010, 10:57am UTC](https://www.ruby-forum.com/t/ruby-on-rails-associations-practical-applications/188327/1 "2010-05-10T10:57:12Z")

</div>

Hey All,  
Forgive me for posting a question that may be very elementary.

I’m building an application and am a bit stuck on how to work out the  
associations. It goes like this:

User :has\_many vehicles  
User :has\_many logs

Vehicle :has\_many logs

User -\> Vehicles -\> Logs

The Vehicles have a user\_id attribute.  
The Logs have user\_id and vehicle\_id attributes.

## I am using restful authentication and am able to associate the vehicles with the users al a

## def create @vehicle = current\_user.vehicle.build (params[:vehicle]) …

I can’t figure out how I should do it for the log.  
Thanks again, and sorry for my lack of understanding of how rails  
works.

Cheers,

Cam Peterson

---

<div class="post-metadata">

**Author:** ![cam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cam](https://www.ruby-forum.com/u/cam)\
**Post date:** [May 10, 2010, 11:14am UTC](https://www.ruby-forum.com/t/ruby-on-rails-associations-practical-applications/188327/2 "2010-05-10T11:14:20Z")

</div>

Cam wrote:

> Hey All,  
> Forgive me for posting a question that may be very elementary.
> 
> I’m building an application and am a bit stuck on how to work out the  
> associations. It goes like this:
> 
> User :has\_many vehicles  
> User :has\_many logs
> 
> Vehicle :has\_many logs
> 
> User -\> Vehicles -\> Logs
> 
> The Vehicles have a user\_id attribute.  
> The Logs have user\_id and vehicle\_id attributes.
> 
> ## I am using restful authentication and am able to associate the vehicles with the users al a
> 
> ## def create @vehicle = current\_user.vehicle.build (params[:vehicle]) …
> 
> I can’t figure out how I should do it for the log.  
> Thanks again, and sorry for my lack of understanding of how rails  
> works.
> 
> Cheers,
> 
> Cam Peterson

In User model write  
has\_many :logs  
has\_many :vehicles

In Vehicle model write  
has\_many :logs

In your logs model write  
belongs\_to :user  
belongs\_to :vehicle

In your vehicle model write  
belongs\_to :user

def create  
@vehicle = current\_user.vehicle.create (params[:vehicle])

---

<div class="post-metadata">

**Author:** ![cam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cam](https://www.ruby-forum.com/u/cam)\
**Post date:** [May 10, 2010, 4:35pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-associations-practical-applications/188327/3 "2010-05-10T16:35:21Z")

</div>

Cam wrote:

> Hey All,  
> Forgive me for posting a question that may be very elementary.
> 
> I’m building an application and am a bit stuck on how to work out the  
> associations. It goes like this:
> 
> User :has\_many vehicles  
> User :has\_many logs
> 
> Vehicle :has\_many logs
> 
> User → Vehicles → Logs
> 
> The Vehicles have a user\_id attribute.  
> The Logs have user\_id and vehicle\_id attributes.
> 
> ## I am using restful authentication and am able to associate the vehicles with the users al a
> 
> ## def create @vehicle = current\_user.vehicle.build (params[:vehicle]) …
> 
> I can’t figure out how I should do it for the log.  
> Thanks again, and sorry for my lack of understanding of how rails  
> works.

You probably want to use has\_many :through. Have you read the Rails  
guide on associations?

> Cheers,
> 
> Cam Peterson

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cam](https://www.ruby-forum.com/u/cam)\
**Post date:** [May 10, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-associations-practical-applications/188327/4 "2010-05-10T22:13:54Z")

</div>

Cam wrote:

> I’m building an application and am a bit stuck on how to work out the  
> associations. It goes like this:
> 
> User :has\_many vehicles  
> User :has\_many logs
> 
> Vehicle :has\_many logs
> 
> User -\> Vehicles -\> Logs
> 
> The Vehicles have a user\_id attribute.  
> The Logs have user\_id and vehicle\_id attributes.

I see two possible scenarios here. Neither is wrong, They depend on  
behavior you want.

1. Users have many vehicles and vehicles have many logs.
2. Users have many vehicles through logs.

In scenario (1) you would need two one-to-many associations:

User  
has\_many :vehicles

Vehicle  
belongs\_to :user  
has\_many :logs

Log  
belongs\_to :user

Logs would now indirectly belong to a user because vehicles belong to  
users. Therefore Log would have only a foreign key to vehicle, but no  
foreign key to user since that would be redundant.

Find the User for a Log with:  
user = my\_log.vehicle.user

In scenario (2) there would be a many-to-many association between User  
and Vehicle with Log joining them.

User  
has\_many :logs  
has\_many :vehicles, :through =\> :logs

Vehicle:  
has\_many :logs  
has\_many :users, :through =\> :logs

Log  
belongs\_to :user  
belongs\_to :vehicle

This scenario could be read as Users have many vehicles and vehicles can  
belong to many users, where logs track the association and can store  
information pertaining to a specific vehicle belonging to a specific  
user.

I’m guessing this is not the scenario you want, but this pattern  
certainly has its uses.
