Forum: Ruby on Rails Stuck on ActiveRecord

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ef31136ad29b9ce06c9f718109a584cc?d=identicon&s=25 Pete (Guest)
on 2006-03-21 16:36

I'm having great fun developing with Rails but I've come to a problem
that I can't  quite figure out.

I have an Order object that "has_many" order_lines. This works perfectly
and I can  see all the lines.

The OrderLine object has an order_id field as well as product_id and

Now ideally I would want to do :


for example to pull back the name of the product it's linking to - but
no matter what I try, I cannot get that to work.

Can anyone tell what I'm doing wrong? I imagine it's pretty obvious....

Thanks in advance!
2c80e1b0837eec76d38d4f7fa1c15e87?d=identicon&s=25 Michael Gorsuch (Guest)
on 2006-03-21 16:41
(Received via mailing list)

OrderLine "belongs_to" "products", right?


If you are doing a Order "has_many" "order_lines", you should call
things this way:


instead of

75ceef25624b3e44943a3b7319ba9612?d=identicon&s=25 Chris Schumann (torgo)
on 2006-03-21 16:43
(Received via mailing list)
Oooh! I might know this.

I'm just a beginner, but what I've done is something like this:


Is there an easier way?

Ef31136ad29b9ce06c9f718109a584cc?d=identicon&s=25 Pete (Guest)
on 2006-03-21 16:45

Sorry, I was using order_lines :)

At present OrderLine belongs to Order
A332dcb1e051f733dc9a9bd78df3b560?d=identicon&s=25 Wes Garrison (Guest)
on 2006-03-21 17:23
(Received via mailing list)
If you have
OrderLine model
  belongs_to :order

...then you'll be able to say:

So, if you want to go the other way:
... you need the association in your Order class so that it knows.  It
doesn't look it the other models for associations that refer to it.

Order model
has_many :order_lines

If you already have this, then you might try checking order, then
orderlines, then product, then name.  Maybe you're missing an
there somewhere.  If that doesn't work, some detail about what you're
actually getting would help.

One other thing that helps me is the Rails console.  You can
make an Order, test its lines, etc all without the continually
cycle of doing it in a web browser.

  --- Wes
81e965af3070ffa9c7979cb5f2494c60?d=identicon&s=25 Hank Marquardt (Guest)
on 2006-03-21 17:32
(Received via mailing list)
For what it's worth I haven't written much rails/ruby, I mostly lurk
... but that said,
Order has many OrderLines has many Products seems like your model and
what you've described, you're missing that final association ...

I guess I'd also think OrderLines would have order_id, product_id and
quantity ... while Products would have unit cost, description, etc ...
Ef31136ad29b9ce06c9f718109a584cc?d=identicon&s=25 Pete (Guest)
on 2006-03-21 17:38
Yes, that's what I've got

the :


bit works perfectly and I can do :


which will give me the product id of a Product in the product table.

But I want to do :


which would point to the product in that line item. Is this possible?
81e965af3070ffa9c7979cb5f2494c60?d=identicon&s=25 Hank Marquardt (Guest)
on 2006-03-21 17:42
(Received via mailing list)
Just to correct myself ... it'd be Order has many OrderLines has one
81e965af3070ffa9c7979cb5f2494c60?d=identicon&s=25 Hank Marquardt (Guest)
on 2006-03-21 17:46
(Received via mailing list)
Does your OrderLine model have a "has_one Product" in it? ... that's
what I
think you're missing even if I'm not syntactically perfect;)
A332dcb1e051f733dc9a9bd78df3b560?d=identicon&s=25 Wes Garrison (Guest)
on 2006-03-21 19:16
(Received via mailing list)
Right, what Hank said, almost.
You've got the
Order model
  has_many :order_lines

You know that it's working because you can get to the product_id, which
on the order line.  Since you can't get to the product, you know  that
OrderLine model doesn't know about the product.

OrderLine model
  belongs_to :product

You want belongs_to because the id for the product is in the order_lines
table.  Once you add that to the OrderLines model, you should be able to
access a product from an order detail.

If you haven't read this:
 ... it's got very good examples of when to use which association.

   -- Wes

On 3/21/06, Hank Marquardt <> wrote:
> > order.orderlines
> >
> >
> >
> _______________________________________________
> Rails mailing list


    -- Wes
81e965af3070ffa9c7979cb5f2494c60?d=identicon&s=25 Hank Marquardt (Guest)
on 2006-03-21 20:51
(Received via mailing list)
That's a handy link for the examples Wes ...  I might have to actually
writing some of this stuff just for fun so I can better grok the syntax
Ef31136ad29b9ce06c9f718109a584cc?d=identicon&s=25 Pete (Guest)
on 2006-03-22 16:33

When I try that I get this error :

undefined method `assert_valid_keys' for :Product:Symbol

I'm going to google for it now :)
This topic is locked and can not be replied to.