# Has\_many :through question

**URL:** <https://www.ruby-forum.com/t/has-many-through-question/157330>\
**Category:** Rails\
**Created:** [January 9, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/has-many-through-question/157330 "2009-01-09T13:42:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [January 9, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/has-many-through-question/157330/1 "2009-01-09T13:42:46Z")

</div>

hi,  
i’m a newbie working on my first commerical rails project. i’ve got some  
models that i’m stuck with, not sure whether i need to redesign the  
schema’s or stick to my earlier design as i can quite get what i want  
from the way i’ve implemented my associations.

Basically i want to access the bookings for a room. the room has\_many  
beds, bed has\_many booking\_items and a booking\_item belongs\_to a  
booking.  
So how can i get the bookings for a bed???  
I know i can do a find\_by\_sql but then i cant use :include and to be  
honest this seems simple enough and i’d rather stick to the rails way so  
to speak and use the association proxies etc.  
Additionally i want to get the rooms for a booking, but i think that is  
the same problem.

I had looked at the nested has many plugin but i managed not to get that  
working (below).

Any help would be really appreciated as i think i’m going to add a  
Room\_Id field to the bookingItem which would mean i could use the  
standard rails associations.

rails version: 2.1.1

schema:

“beds”  
integer “room\_id”  
integer “bed\_number”

“booking\_items”  
integer “booking\_id”  
integer “bed\_id”  
integer “price”  
date “booking\_date”

“bookings”  
integer “user\_id”  
integer “amount”

“rooms”  
integer “bed\_quantity”  
integer “floor”  
string “room\_name”

models:

class Bed \< ActiveRecord::Base  
belongs\_to :room  
has\_many :booking\_items  
end

class BookingItem \< ActiveRecord::Base  
belongs\_to :booking  
belongs\_to :user  
belongs\_to :bed  
end

class Booking \< ActiveRecord::Base  
has\_many :booking\_items, :dependent =\> :destroy  
has\_many :payments  
has\_many :beds, :through =\> :booking\_items

```
has_many :rooms, :through => :beds # THIS IS NOT WORKING

```

end

class Room \< ActiveRecord::Base  
has\_many :beds  
has\_many :booking\_items, :through =\> :beds

```
has_many :bookings, :through => :booking_items, :source =>

```

:booking\_items  
# THIS IS NOT WORKING, I TRIED TO USE NESTED HAS MANY HERE.  
end

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [January 9, 2009, 2:01pm UTC](https://www.ruby-forum.com/t/has-many-through-question/157330/2 "2009-01-09T14:01:58Z")

</div>

in your Bed-model add  
has many :bookings, :through =\> :booking\_items

then you should be able to call  
bookings\_for\_bed\_1 = Bed.find(1).bookings

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [January 9, 2009, 7:47pm UTC](https://www.ruby-forum.com/t/has-many-through-question/157330/3 "2009-01-09T19:47:40Z")

</div>

Hi,

I added that and could get the bookings for a bed. but i wanted to get  
the bookings for a room using an association like:

room.bookings

i tried adding this using the nested has many plugin:

has\_many :bookings, :through =\> :booking\_items

to Room. but i got this error message.

Mysql::Error: Unknown column  
'booking\_items.roomActiveRecord::StatementInvalid: Mysql::Error: Unknown  
column ‘booking\_items.room\_id’ in ‘where clause’: SELECT `bookings`.\*  
FROM `bookings` INNER JOIN booking\_items ON bookings.id =  
booking\_items.booking\_id WHERE ((`booking_items`.room\_id = 1))

Am i better off just adding Room\_Id to the BookingItem model and  
effectively denormalising it??

Thanks for the help.
