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

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

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 ActiveRecord::Associations::ClassMethods 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