Claudio Poli
2008-Jul-09 01:37 UTC
extra field as attribute in join table with has_many :through
hi everyone, I''ve pasted some code over there http://pastie.org/230343 . what I find strange is that the extra field in the join table called "quantity" doesn''t shows up in the Drug resultset, even if I added an attr_accessor for good measure. anybody knows why? I''m on edge rails. thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jul-09 09:27 UTC
Re: extra field as attribute in join table with has_many :through
On Jul 9, 2:37 am, Claudio Poli <masterk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hi everyone, > I''ve pasted some code over therehttp://pastie.org/230343. > > what I find strange is that the extra field in the join table called > "quantity" doesn''t shows up in the Drug resultset, even if I added an > attr_accessor for good measure. >The attr_accessor would actually squash the attribute if it were there. It''s normal not to get the join table attributes - that''s just the way rails does things. Your workaround with the explicit select clause should work, I expect that you are being fooled by ActiveRecord''s display of your instance of Drug, which only displays attributes from the drugs.table. Document.find(2).drugs.first.quantity should work (but get rid of the attr_accessor first). Fred> anybody knows why? I''m on edge rails. > > thanks.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matthew Rudy Jacobs
2008-Jul-09 15:28 UTC
Re: extra field as attribute in join table with has_many :through
Frederick Cheung wrote:> On Jul 9, 2:37�am, Claudio Poli <masterk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> hi everyone, >> I''ve pasted some code over therehttp://pastie.org/230343. >> >> what I find strange is that the extra field in the join table called >> "quantity" doesn''t shows up in the Drug resultset, even if I added an >> attr_accessor for good measure. >> > The attr_accessor would actually squash the attribute if it were > there. > It''s normal not to get the join table attributes - that''s just the way > rails does things. Your workaround with the explicit select clause > should work, I expect that you are being fooled by ActiveRecord''s > display of your instance of Drug, which only displays attributes from > the drugs.table. Document.find(2).drugs.first.quantity should work > (but get rid of the attr_accessor first). > > FredOne way to guarantee the method exists, and allow you to cast it to an integer would be; class Drug < ActiveRecord::Base def quantity self[:quantity].to_i end end But I don''t like it, because "quantity" isn''t actually a value of of the Drug, it''s a value of the join. clearly; drug = document.drugs.first drug.quantity != drug.reload.quantity (which will always be 0) I''d prefer the following syntax; drug = documents.drugs.first quantity = documents.quantity_of_drug(drug) class Document < ActiveRecord::Base def quantity_of_drug(drug) if join = join_for_drug(drug) return join.quantity else return 0 end end private def join_for_drug(drug) self.document_drugs.detect {|dd| dd.drug == drug} end end however you''d need to ensure you loaded drugs through document_drugs, rather than by a direct join (not quite sure what rails does about this at the moment) -- 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 -~----------~----~----~----~------~----~------~--~---
Claudio Poli
2008-Jul-11 16:52 UTC
Re: extra field as attribute in join table with has_many :through
thanks for the suggestions, I''m going to do a bit of work on that as advertised. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---