Steven D. Arnold
2008-Mar-22 20:50 UTC
has_many relationship not providing my model with attribute=
I have a Rails app that tracks service requests (SRs). A user can observe many service requests, and a service request can be observed by many users, hence it''s a many-to-many relationship. I have a table for this defined as follows: create_table :sr_user_roles do |t| t.column :user_id, :integer t.column :sr_id, :integer t.column :role_type_id, :integer end Note that the particular relationship of the user to the service request can vary, and I need to keep track of that in the many-to-many table. Therefore, I don''t think HABTM will suit my needs. Therefore I use has_many in both the User and Sr models: class Sr < ActiveRecord::Base has_many :sr_user_roles, :foreign_key => :sr_id has_many :observers, :through => :sr_user_roles, :source => :user [...] end class User < ActiveRecord::Base has_many :sr_user_roles, :foreign_key => :user_id has_many :observed_srs, :through => :sr_user_roles, :source => :sr [...] end The code for the many-to-many model is below: class SrUserRole < ActiveRecord::Base belongs_to :sr belongs_to :user [...] end In my code, I can print out anSr.observers in a debug statement. However, when I try to assign to anSr.observers, I get the following error: NoMethodError in SrsController#update undefined method `observers='' for #<Sr:0x2000af8> Obviously I''m dealing with an Sr object from the above error, yet it doesn''t seem to know about observers=. Here''s the code that causes the above error: logger.info "@sr.observers = #{@sr.observers}" # This works # The assignment below does not work @sr.observers = params[''sr_user_role''].values.collect {|v| User.find(v) if not v.empty?} I''m using Rails 1.2.6. Any idea what might be going wrong here? steven --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Steven D. Arnold
2008-Apr-01 05:25 UTC
Re: has_many relationship not providing my model with attribute=
Following up on my previous post on has_many issues, I have upgraded to rails 2.0.2 in an attempt to solve my has_many problems. The current issue is that the collection_singular_ids syntax as documented at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001103 does not seem to work for me. I have code that looks like this: class Sr < ActiveRecord::Base has_many :observers, :through => :sr_user_roles, :source => :user In the controller, if I understand correctly, I should be able to do this: @sr.observer_ids = <array of values> Yet I get the error: undefined method `observer_ids='' for #<Sr:0x26a0354> Interestingly, Rails seems to have defined observer=, even though it doesn''t seem to change the contents of the database when I assign something using the observer= mechanism. I''ve been working on this problem for quite a while and just can''t figure it out. Is this a bug? Has anyone actually made this work for them? steven --~--~---------~--~----~------------~-------~--~----~ 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-Apr-01 09:04 UTC
Re: has_many relationship not providing my model with attribute=
On 1 Apr 2008, at 06:25, Steven D. Arnold wrote:> > Following up on my previous post on has_many issues, I have upgraded > to rails 2.0.2 in an attempt to solve my has_many problems. The > current issue is that the collection_singular_ids syntax as documented > at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001103 > does not seem to work for me. I have code that looks like this: >It doesn''t work for has_many :through. If you look right at the top of the link you gave there''s a handle table of which association methods you get when. Fred> class Sr < ActiveRecord::Base > has_many :observers, :through => :sr_user_roles, :source => :user > > In the controller, if I understand correctly, I should be able to do > this: > > @sr.observer_ids = <array of values> > > Yet I get the error: > > undefined method `observer_ids='' for #<Sr:0x26a0354> > > Interestingly, Rails seems to have defined observer=, even though it > doesn''t seem to change the contents of the database when I assign > something using the observer= mechanism. > > I''ve been working on this problem for quite a while and just can''t > figure it out. Is this a bug? Has anyone actually made this work for > them? > > steven > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---