I''ve setup two models, 1 and 2, that are associated by has_and_belongs_to_many. I''m trying to get an attribute from model_1 to use in a method in model_2. When I use the code below, I get an error saying ''undefined method model_1_id''. What am I missing? Thanks! Model_2.rb Class Model_2 < ActiveRecord::Base ... has_and_belongs_to_many :model_1 def some_method attr_a * Model_1.find(model_1_id).attr_I_need end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit groups.google.com/d/msg/rubyonrails-talk/-/Pvbh1t-J4qMJ. For more options, visit groups.google.com/groups/opt_out.
On Tue, Sep 4, 2012 at 9:53 AM, sacshu <sacshu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> When I use the code below, I get an error saying > ''undefined method model_1_id''. What am I missing? Thanks!> Class Model_2 < ActiveRecord::Base > ... > has_and_belongs_to_many :model_1 > > def some_method > attr_a * Model_1.find(model_1_id).attr_I_need > endWhere do you think ''model_1_id'' is being defined here? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org about.me/hassanschroeder twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit groups.google.com/groups/opt_out.
Well that''s kind of what I need help with, I''m not sure where it needs to be defined in a many-to-many relationship. I''ve already created the join table that contains both ids. -- Posted via 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit groups.google.com/groups/opt_out.
On Wed, Sep 5, 2012 at 1:00 PM, Sa S. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Well that''s kind of what I need help with, I''m not sure where it needs > to be defined in a many-to-many relationship. I''ve already created the > join table that contains both ids.The point is that your example makes no sense; you''ve defined this as a "to-many" relationship -- which Model_1 are you looking for? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org about.me/hassanschroeder twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit groups.google.com/groups/opt_out.
Hassan Schroeder wrote in post #1074840:> On Wed, Sep 5, 2012 at 1:00 PM, Sa S. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Well that''s kind of what I need help with, I''m not sure where it needs >> to be defined in a many-to-many relationship. I''ve already created the >> join table that contains both ids. > > The point is that your example makes no sense; you''ve defined this > as a "to-many" relationship -- which Model_1 are you looking for? > > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > about.me/hassanschroeder > twitter: @hassanSorry, I should clarify. In my Model_2 view, I''m trying to simply have a table that iterates through all the model_1''s and calculates that method above using the model_1''s attribute that I need -- Posted via 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit groups.google.com/groups/opt_out.
On Wed, Sep 5, 2012 at 1:14 PM, Sa S. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Sorry, I should clarify. In my Model_2 view, I''m trying to simply have a > table that iterates through all the model_1''s and calculates that method > above using the model_1''s attribute that I needBut you''re not showing us a view, you''re showing us a model. Do you really want `some_method` to return one value or an array of values? If you only want one value, why don''t you pass the Model_1 instance (or the attribute) you''re interested in to the Model_2 instance? def some_method(model_1_attribute) attr_a * model_1_attribute end -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org about.me/hassanschroeder twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit groups.google.com/groups/opt_out.