I know, I have probably a wrong design. Just need your point of view on that. Here is Police has many products Police has many transactions Products has many transactions I need to get a transaction with the combination of police and product. Is it feasible to defines associations to do that. I know that''s weird a bit. I think I''m missing a model in between. Rémi -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 4, 3:05 pm, Rémi Gagnon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I know, I have probably a wrong design. Just need your point of view on > that. > > Here is > > Police has many products > Police has many transactions > Products has many transactions > > I need to get a transaction with the combination of police and product. > > Is it feasible to defines associations to do that. I know that''s weird > a bit. I think I''m missing a model in between. > > Rémi > -- > Posted viahttp://www.ruby-forum.com/.I think the design is fine, actually. If I understand correctly, your transactions table has columns police_id and product_id (and others). If so, you can find a transaction this way: Transaction.find_by_police_and_product(police, product) This will return the first one that matches. You can also add further conditions, ordering, etc. as with any other find. Jeff www.purpleworkshops.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you. But is there a way to navigate that relationship, not by a find?> I think the design is fine, actually. If I understand correctly, your > transactions table has columns police_id and product_id (and others). > > If so, you can find a transaction this way: > > Transaction.find_by_police_and_product(police, product) > > This will return the first one that matches. You can also add further > conditions, ordering, etc. as with any other find. > > Jeff > > www.purpleworkshops.com-- 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-/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 -~----------~----~----~----~------~----~------~--~---
On Dec 5, 7:26 am, Rémi Gagnon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thank you. But is there a way to navigate that relationship, not by a > find?Not sure I understand the question exactly... you can use has_many and belongs_to to help navigate: class Police has_many :transactions has_many :products end class Transaction belongs_to :police belongs_to :product end class product has_many :transactions belongs_to :police end For example: p = Product.find(1) You can do: p.transactions p.transactions.first.police Similarly t = Transaction.last t.products t.products.first.police Or maybe I misunderstood your question? Jeff www.purpleworkshops.com> > I think the design is fine, actually. If I understand correctly, your > > transactions table has columns police_id and product_id (and others). > > > If so, you can find a transaction this way: > > > Transaction.find_by_police_and_product(police, product) > > > This will return the first one that matches. You can also add further > > conditions, ordering, etc. as with any other find. > > > Jeff > > >www.purpleworkshops.com > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Jeff, I was not very clear in my question. I would like to do Police.transaction but in conjunction with Product. But anyway today we redesign our model to make it cleaner. Thanks for your help. Rémi Jeff Cohen wrote:> On Dec 5, 7:26�am, R�mi Gagnon <rails-mailing-l...@andreas-s.net> > wrote: >> Thank you. �But is there a way to navigate that relationship, not by a >> find? > > Not sure I understand the question exactly... you can use has_many and > belongs_to to help navigate: > > class Police > has_many :transactions > has_many :products > end > > class Transaction > belongs_to :police > belongs_to :product > end > > class product > has_many :transactions > belongs_to :police > end > > For example: > > p = Product.find(1) > > You can do: > > p.transactions > p.transactions.first.police > > Similarly > > t = Transaction.last > t.products > t.products.first.police > > Or maybe I misunderstood your question? > > Jeff > > www.purpleworkshops.com-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---