# Many to Many questions

**URL:** <https://www.ruby-forum.com/t/many-to-many-questions/88207>\
**Category:** Rails\
**Created:** [March 8, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/many-to-many-questions/88207 "2007-03-08T22:55:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rordirectory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rordirectory](https://www.ruby-forum.com/u/rordirectory)\
**Post date:** [March 8, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/many-to-many-questions/88207/1 "2007-03-08T22:55:20Z")

</div>

Hi  
I have the following tables

Ads - contains ads by people  
Options - contains options (all are yes or no like a check box) for  
each ad; examples (assuming it is a car ad) Air\_condition,  
Auto\_Transmission, Keyless\_entry, etc.

Each option will have a yes or no value.

the table ad\_options will contain actual values for each ad for the  
option;

class Ad \< ActiveRecord::Base  
has\_many :options, :through =\> :ad\_options  
end

Is the above statement correct? if yes how can I use the mtm  
relationship in views to show and update options. This will give me  
the flexibility to add options without changing the code.

Thanks a lot for your help  
rt

---

<div class="post-metadata">

**Author:** ![rordirectory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rordirectory](https://www.ruby-forum.com/u/rordirectory)\
**Post date:** [March 9, 2007, 3:51am UTC](https://www.ruby-forum.com/t/many-to-many-questions/88207/2 "2007-03-09T03:51:55Z")

</div>

How many tables do you have and what columns have you defined in it?  
Without knowing what data you are capturing it is not possible to  
check if what you are doing is correct.

You can show the options as check boxes that users can select if they  
want. This will be under the ad that they want to create.

---

<div class="post-metadata">

**Author:** ![rordirectory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rordirectory](https://www.ruby-forum.com/u/rordirectory)\
**Post date:** [March 9, 2007, 11:06am UTC](https://www.ruby-forum.com/t/many-to-many-questions/88207/3 "2007-03-09T11:06:32Z")

</div>

Presumably, you would want to have an ad\_types table… otherwise,  
what’s  
the point in an ad having an “air conditioning” option when it’ll be  
false?

So, assuming I’ve understood correctly:

Ad  
belongs\_to :ad\_type  
has\_many :options, :through =\> values  
end

AdType  
has\_many :ads  
has\_many :options # (or maybe habtm would be better)  
end

Option  
belongs\_to :ad\_type  
has\_many :values  
has\_many :ads, :through =\> :values  
end

Value  
belongs\_to :option  
belongs\_to :ad  
end

You’re options table would contain the strings such as “air  
conditioning”,  
and the values would contain the boolean value of an option for it’s ad.

I think I’ve covered everything there. That’s the best way I can think  
of to  
do what you’re trying to do. There’s a loop in the table relationships,  
which might cause a few problems in writing the model, but nothing that  
can’t be solved with a bit of work. For example, an ad must have the  
same  
options when going round one way as when going the other way. You’re  
model  
should make sure that this integrity is maintained.

Hope this helps. If anyone reckons this is completely the wrong way of  
doing  
it, please say so.

-N
