# Some table structure advice?

**URL:** <https://www.ruby-forum.com/t/some-table-structure-advice/105353>\
**Category:** Rails\
**Created:** [May 29, 2007, 7:54am UTC](https://www.ruby-forum.com/t/some-table-structure-advice/105353 "2007-05-29T07:54:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Davo](https://www.ruby-forum.com/u/Davo)\
**Post date:** [May 29, 2007, 7:54am UTC](https://www.ruby-forum.com/t/some-table-structure-advice/105353/1 "2007-05-29T07:54:00Z")

</div>

Hi everyone,

I’m just starting a new project and it needs this type of structure:

an item has a brand and the brand has a number of models.

( e.g. The item might be an Advert for a Honda ( brand ), Civic  
( model )

So in the admin maintenance I need to set up all the brands and models  
I need.

In the public area there needs to be a selection first for the brand  
then the model selection, which will only show the models for the  
selected brand.

In the advert table I could just have a belongs\_to model ( as the  
model belongs\_to :brand ).

But I’m not sure of the best approach.

Hope this makes sense.

Also can someone point me in the right direction to do the model drop  
down selection based on the previous selected brand ?

TIA - Dave P.

---

<div class="post-metadata">

**Author:** ![Davo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Davo](https://www.ruby-forum.com/u/Davo)\
**Post date:** [May 29, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/some-table-structure-advice/105353/2 "2007-05-29T14:53:52Z")

</div>

Hi Dave . . .

On May 29, 1:53 am, Davo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> an item has a brand and the brand has a number of models.

That sounds like this to me:

class Item \< ActiveRecord::Base  
has\_many :brands  
end

class Brand \< ActiveRecord::Base  
belongs\_to :item  
has\_many :models  
end

class Model \< ActiveRecord::Base  
belongs\_to :brand  
end

I see the migrations looking like this (I’ve cut out a lot of  
boilerplate for brevity):

class CreateItems \< ActiveRecord::Migration  
// Nothing special needed  
end

class CreateBrands \< ActiveRecord::Migration  
…  
t.column :name, :string  
t.column :item\_id, :int  
…  
end

class CreateModels \< ActiveRecord::Migration  
…  
t.column :name, :string  
t.column :brand\_id, :int  
…  
end

> Also can someone point me in the right direction to do the model drop  
> down selection based on the previous selected brand ?

Sure. Take a look at the observe\_field method we get with Rails +  
Prototype:

[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000535](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000535)

Don’t forget to put \<% javascript\_include\_tag :defaults %\> in your  
form rhtml if you’re gonnna use observe\_field. You basically want to  
observe the drop-down for the brand, probably with a :frequency =\> 0  
so that it fires whenever a new brand is selected. In your controller  
you need an action that observe\_field can call that returns the  
contents of the model drop-down (which will be the value for  
observe\_field’s :update param). Use the :with param to assign a name  
to the value that’s chosen in your brand drop-down (something  
like :with =\> ‘chosen\_brand’).

In your controller’s action, you’re going to do something along these  
lines:

@models = Brand.find(params[:chosen\_brand]).models  
render :partial =\> ‘populate\_model\_select’

This assumes that your brands drop-down has been populated from the  
DB, and the values of the options are the brand ID from the database  
(check out collection\_select in ActionView::Helpers::FormOptionsHelper  
to accomplish that).

Then in your \_populate\_model\_select.rhtml file, you’ll have something  
like:

\<% @models.each do |m| %\>

 \<%= m.name %\> \<% end %\>

That oughtta get you started down the right path. 🙂

---

<div class="post-metadata">

**Author:** ![Davo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Davo](https://www.ruby-forum.com/u/Davo)\
**Post date:** [May 30, 2007, 1:48am UTC](https://www.ruby-forum.com/t/some-table-structure-advice/105353/3 "2007-05-30T01:48:02Z")

</div>

Thanks Bill,

I’ll work through that over the next few days and let youi know how I  
go.

regards, Dave
