# Many to many questions

**URL:** <https://www.ruby-forum.com/t/many-to-many-questions/56523>\
**Category:** Rails\
**Created:** [December 2, 2005, 2:54pm UTC](https://www.ruby-forum.com/t/many-to-many-questions/56523 "2005-12-02T14:54:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![papipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@papipo](https://www.ruby-forum.com/u/papipo)\
**Post date:** [December 2, 2005, 2:54pm UTC](https://www.ruby-forum.com/t/many-to-many-questions/56523/1 "2005-12-02T14:54:26Z")

</div>

Hello Railers!

I have 3 models: Hotel, Room and Booking

Hotel has\_many :rooms, and has\_many :bookings  
Room belongs\_to :hotel, and habtm :bookings  
Booking belongs\_to :hotel, and habtm :rooms

i wish to add a condition to the @booking.rooms, they should share the  
same  
hotel.id as the booking and the collection itself, since a booking is  
made  
on a single hotel, not many. Â¿How can i accomplish that? It should be  
something like :conditions =\> “hotel\_id = self.hotel.id” (of course,  
this  
doesn’t work).

Another thing is that i don’t know if the operator “\<\<” can be  
redefined. If  
i redefine Booking.rooms \<\< in some way, i could check if the collection  
is  
empty, and assign the hotel\_id of the room beign added to the collection  
to  
the booking.hotel.

The third thing/question/approach is that another way is to make Booking  
a  
“virtual” model, not beign saved directly until payment made, as Cart in  
the  
Agile dev with rails book.

Please, could you give me some advices?

Thanks,  
Rodrigo.

---

<div class="post-metadata">

**Author:** ![papipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@papipo](https://www.ruby-forum.com/u/papipo)\
**Post date:** [December 2, 2005, 7:30pm UTC](https://www.ruby-forum.com/t/many-to-many-questions/56523/2 "2005-12-02T19:30:50Z")

</div>

i think this is what you are looking for.

class Booking \< ActiveRecord::Base  
belongs\_to :hotel

# this says that rooms.hotel\_id has to match the hotel\_id attribute of

the  
instantiated booking object

# note the use of single quotes around the sql fragement, this is

required  
if using #{attr} in the condition  
has\_and\_belongs\_to\_many :rooms, :conditions =\> ‘hotel\_id =  
#{hotel\_id}’  
end

---

<div class="post-metadata">

**Author:** ![papipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@papipo](https://www.ruby-forum.com/u/papipo)\
**Post date:** [December 2, 2005, 9:17pm UTC](https://www.ruby-forum.com/t/many-to-many-questions/56523/3 "2005-12-02T21:17:24Z")

</div>

On 12/2/05, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> That looks really good! i’ll give it a try, thanks a lot.  
> Apart from that, is really possible yo redefine the “\<\<” method/operator  
> for  
> a collection?

Thanks again.

---

<div class="post-metadata">

**Author:** ![papipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@papipo](https://www.ruby-forum.com/u/papipo)\
**Post date:** [December 2, 2005, 9:37pm UTC](https://www.ruby-forum.com/t/many-to-many-questions/56523/4 "2005-12-02T21:37:45Z")

</div>

On 12/2/05, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end

is this documented anywhere? (the use of attributes in :conditions)

---

<div class="post-metadata">

**Author:** ![papipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@papipo](https://www.ruby-forum.com/u/papipo)\
**Post date:** [December 7, 2005, 3:32am UTC](https://www.ruby-forum.com/t/many-to-many-questions/56523/5 "2005-12-07T03:32:15Z")

</div>

I’ve seen it used in the associations documentation examples, but there  
is  
nothing that i have found that actually describes usage. this is not to  
say  
that it isn’t documented, i just haven’t seen it.
