Hello, how is it possible to specify a "has_one" attribut in an one-to-many relationship? This code doesn''t work: class Order < ActiveRecord::Base has_and_belongs_to_many :invoices has_one :most_recent_invoice, :order => ''date DESC'' end Thanks and best regards, Linus --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
For this type of question you need to include the migrations for both tables for anyone to be able to help you, and the invoice class as well. Do you have an order_id in the invoice table for the has one relation? Does the invoice have a belongs_to :order association defined? Michael On Apr 25, 12:18 am, Linus Mueller <linus.muel...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hello, > > how is it possible to specify a "has_one" attribut in an one-to-many > relationship? > This code doesn''t work: > > class Order < ActiveRecord::Base > has_and_belongs_to_many :invoices > has_one :most_recent_invoice, :order => ''date DESC'' > end > > Thanks and best regards, > Linus--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That is the problem. I have no order_id in the invoice table. I have only the relationship table invoices_orders with the foreign keys. Class Inoice: class Invoice < ActiveRecord::Base has_and_belongs_to_many :orders end Linus On 25 Apr., 16:47, MichaelLatta <lat...-ee4meeAH724@public.gmane.org> wrote:> For this type of question you need to include the migrations for bothtablesfor anyone to be able to help you, and the invoice class as > well. > > Do you have an order_id in the invoice table for the has one relation? > Does the invoice have a belongs_to :order association defined? > > Michael > > On Apr 25, 12:18 am, Linus Mueller <linus.muel...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > wrote: > > > Hello, > > > how is it possible to specify a "has_one" attribut in an one-to-many > >relationship? > > This code doesn''t work: > > > class Order < ActiveRecord::Base > > has_and_belongs_to_many :invoices > > has_one :most_recent_invoice, :order => ''date DESC'' > > end > > > Thanks and best regards, > > Linus--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, I would extend the habtm association: class Order < ActiveRecord::Base has_and_belongs_to_many :invoices do def most_recent find :first, :order => "date DESC" end end end Simon Linus Mueller wrote:> Hello, > > how is it possible to specify a "has_one" attribut in an one-to-many > relationship? > This code doesn''t work: > > class Order < ActiveRecord::Base > has_and_belongs_to_many :invoices > has_one :most_recent_invoice, :order => ''date DESC'' > end > > > Thanks and best regards, > Linus > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Simon! That''s a great solution! Thank you:-) Linus On 26 Apr., 21:28, Simon Pasquier <pasquier.si...-GANU6spQydw@public.gmane.org> wrote:> Hi, > > I would extend the habtm association: > > class Order < ActiveRecord::Base > has_and_belongs_to_many :invoices do > def most_recent > find :first, :order => "date DESC" > end > end > end > > Simon > > Linus Mueller wrote: > > Hello, > > > how is it possible to specify a "has_one" attribut in an one-to-many > >relationship? > > This code doesn''t work: > > > class Order < ActiveRecord::Base > > has_and_belongs_to_many :invoices > > has_one :most_recent_invoice, :order => ''date DESC'' > > end > > > Thanks and best regards, > > Linus--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---