Mike Buckley - wrote:>
>
> I have this application that I want to move to RoR. It is somewhat
> basic, but the inheritance always gets me (be patient, this is a newbie
> here, trying to learn this language, so go easy...) I have a table with
> customer data, with a primary key (id), I also have another table
> called ''vehicles'' which has a relationship to customer,
where customer
> has many vehicles, and vehicles has one customer
>
> What I want to do, and it seems my model is not configured right is to
> have my show screen ''show'' the customer information, and
below, a
> subform if you will, display associated vehicle info, which could be 1
> to say 5 cars....
>
> I can''t seem to force ROR to return the associated vehicle that is
> associated with the customer id. no matter what I seem to do, it
> returns the customer id, or actually in this case, the primary key of
> the vehicles table.
>
> Any suggestions? I also have a couple of more relations to deal with,
> but think once I can do this, I can take it to the next level...
>
Hi Mike,
Have you set-up the relationships within the Models themselves? If not,
you will need to add the following to your Customer model
has_many :vehicles
and the following to your Vehicle model
belongs_to :customer
In the show action of your Customer controller, you should already be
populating a @customer using something like Customer.find(params[:id]).
Because of the relationships set-up in the models, this Customer will
have a vehicles method that returns an array of any vehicles associated
with that Customer. In your show view you already have access to your
@customer and therefore also their vehicles. You should be able to do
something like this in your view to display a list of all vehicle makes.
<% @customer.vehicles.each { |vehicle| %>
<%= vehicle.make %><br/>
<% } %>
In this instance, I am assuming that you have a field in the vehicles
db table called make but that can, of course, be changed to a valid
field in your table.
Hope this helps,
Chris