Hi, 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 amount. Now ideally I would want to do : order.orderline[0].product.name 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! -- Posted via http://www.ruby-forum.com/.
Hmm... OrderLine "belongs_to" "products", right? Also: If you are doing a Order "has_many" "order_lines", you should call things this way: order.order_lines... instead of order.orderline... On 3/21/06, Pete <miggyx@peteslan.net> wrote:> Hi, > > 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 > amount. > > Now ideally I would want to do : > > > order.orderline[0].product.name > > 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! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Oooh! I might know this. I''m just a beginner, but what I''ve done is something like this: Product.find(orderline[0].product_id).name Is there an easier way? Chris wrote:> Hi, > > 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 > amount. > > Now ideally I would want to do : > > > order.orderline[0].product.name > > 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! >
Hi, Sorry, I was using order_lines :) At present OrderLine belongs to Order -- Posted via http://www.ruby-forum.com/.
If you have OrderLine model belongs_to :order ...then you''ll be able to say: @the_order_line.order So, if you want to go the other way: @the_order.order_line ... 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 association 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 interactively make an Order, test its lines, etc all without the continually save/refresh cycle of doing it in a web browser. --- Wes On 3/21/06, Pete <miggyx@peteslan.net> wrote:> > Hi, > > Sorry, I was using order_lines :) > > At present OrderLine belongs to Order > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060321/8e915ac5/attachment.html
For what it''s worth I haven''t written much rails/ruby, I mostly lurk here ... but that said, Order has many OrderLines has many Products seems like your model and from 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 ... On 3/21/06, Wes Garrison <wes.garrison@gmail.com> wrote:> > If you have > OrderLine model > belongs_to :order > > ...then you''ll be able to say: > @the_order_line.order > > So, if you want to go the other way: > @the_order.order_line > ... 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 association > 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 interactively > make an Order, test its lines, etc all without the continually save/refresh > cycle of doing it in a web browser. > > --- Wes > > On 3/21/06, Pete <miggyx@peteslan.net> wrote: > > > Hi, > > > > Sorry, I was using order_lines :) > > > > At present OrderLine belongs to Order > > > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060321/cfbdb1e2/attachment.html
Yes, that''s what I''ve got the : order.orderlines bit works perfectly and I can do : order.orderlines[0].product_id which will give me the product id of a Product in the product table. But I want to do : order.orderlines[0].product which would point to the product in that line item. Is this possible? -- Posted via http://www.ruby-forum.com/.
Just to correct myself ... it''d be Order has many OrderLines has one Product On 3/21/06, Hank Marquardt <hmarquardt@gmail.com> wrote:> > For what it''s worth I haven''t written much rails/ruby, I mostly lurk here > ... but that said, > Order has many OrderLines has many Products seems like your model and from > 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 ... > > > > On 3/21/06, Wes Garrison <wes.garrison@gmail.com> wrote: > > > If you have > OrderLine model > belongs_to :order > > ...then you''ll be able to say: > @the_order_line.order > > So, if you want to go the other way: > @the_order.order_line > ... 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 association > 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 interactively > make an Order, test its lines, etc all without the continually save/refresh > cycle of doing it in a web browser. > > --- Wes > > On 3/21/06, Pete <miggyx@peteslan.net> wrote: > > > Hi, > > > > Sorry, I was using order_lines :) > > > > At present OrderLine belongs to Order > > > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > _______________________________________________ > > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060321/c6eeaa29/attachment.html
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;) On 3/21/06, Pete <miggyx@peteslan.net> wrote:> > Yes, that''s what I''ve got > > the : > > order.orderlines > > > bit works perfectly and I can do : > > order.orderlines[0].product_id > > > which will give me the product id of a Product in the product table. > > But I want to do : > > order.orderlines[0].product > > > which would point to the product in that line item. Is this possible? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060321/8363c84c/attachment.html
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 is on the order line. Since you can''t get to the product, you know that the 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: http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html ... it''s got very good examples of when to use which association. -- Wes On 3/21/06, Hank Marquardt <hmarquardt@gmail.com> wrote:> > 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;) > > On 3/21/06, Pete <miggyx@peteslan.net> wrote: > > > Yes, that''s what I''ve got > > > > the : > > > > order.orderlines > > > > > > bit works perfectly and I can do : > > > > order.orderlines[0].product_id > > > > > > which will give me the product id of a Product in the product table. > > > > But I want to do : > > > > order.orderlines[0].product > > > > > > which would point to the product in that line item. Is this possible? > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- -- Wes -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060321/9d9705ad/attachment.html
That''s a handy link for the examples Wes ... I might have to actually start writing some of this stuff just for fun so I can better grok the syntax more naturally. On 3/21/06, Wes Garrison <wes.garrison@gmail.com> wrote:> > 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 is > on the order line. Since you can''t get to the product, you know that the > 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: > > http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html > ... it''s got very good examples of when to use which association. > > -- Wes > > On 3/21/06, Hank Marquardt <hmarquardt@gmail.com> wrote: > > > 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;) > > On 3/21/06, Pete <miggyx@peteslan.net> wrote: > > > Yes, that''s what I''ve got > > > > the : > > > > order.orderlines > > > > > > bit works perfectly and I can do : > > > > order.orderlines[0].product_id > > > > > > which will give me the product id of a Product in the product table. > > > > But I want to do : > > > > order.orderlines[0].product > > > > > > which would point to the product in that line item. Is this possible? > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > -- > > -- Wes > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060321/0bf3aef5/attachment-0001.html