Many to Many questions


#1

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


#2

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.


#3

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