Ruby on Rails Tuesday, July 31, 2012

On 1 August 2012 06:04, John Blaze <lists@ruby-forum.com> wrote:
> Hi,
>
> I am new to RoR and have been pulling my hair out trying to figure out
> associations.
>
> Lets say I have 2 tables: vehicles and vehicle_types. ONE vehicle HAS
> ONE vehicle_type. Therefore, in my opinion, vehicle_type belongs_to
> vehicle.

No, the names of associations are confusing at times. In your case a
vehicle type can be associated with many vehicles, therefore
vehicle_type has_many vehicles. A vehicle is only of one type, so
vehicle belongs_to vehicle_type, and it is the vehicle that has the
vehicle_type_id.

Colin

>
> Hence, vehicle_types will contain the vehicle foreign key. To me this is
> already weird. I would like vehicle_types to be a table containing a
> column that lists types of vehicles- "truck", "car", "bus", etc. It does
> not need to know what vehicle is associated with it. The vehicles table
> needs to know what vehicle_type it is.
>
> I would like to use the vehicle_types table to populate a Select Menu on
> a form. If I create the associations as suggested by RoR, with the
> foreign key going on the belongs_to table (vehicle_types), the Select
> Menu would be populated with duplicate vehicle_types. EG:
>
> vehicle_id vehicle_type_id type
> 1 1 car
> 2 2 truck
> 3 3 car
>
> Does this make sense?
>
> Like I said I have spent days reading over the associations again and
> again but I just can't figure it out. Any help would be greatly
> appreciated.
>
> Thanks.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

1 comment:

Unknown said...

Ruby on Rails is a free open-source web development framework based on MVC (Model View Controller) architecture that is used to develop faster and high performing web applications with Agile Development Methodology.

Ruby on Rails | Ruby on Rails Development | Rails Development

Post a Comment