ArgumentError in has_many :through

I’ve got an application that I’m working on where I have a System model
that I want to add to arbitrary logical Groups (a System can be part of
multiple groups and a Group is made up of one or more Systems).

I’ve got it set up like this:

class Affiliation < ActiveRecord::Base
belongs_to :systems, :class_name => ‘system’, :foreign_key =>
‘system_id’
belongs_to :groups, :class_name => ‘group’, :foreign_key => ‘group_id’
end

class Group < ActiveRecord::Base
has_many :affiliations, :dependent => true
has_many :systems, :through => :affiliations
end

class System < ActiveRecord::Base
has_many :affiliations, :dependent => true
has_many :groups, :through => :affiliations
end

when I do this (in my add action in my GroupsController:

group = Group.find(params[:group], :limit => 1)
system = System.find(params[:system], :limit => 1)
group.systems << system

I get an ArgumentError: wrong number of arguments.

the stack trace is:

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1360:in
system' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1360:incompute_type’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/reflection.rb:125:in
send' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/reflection.rb:125:inklass’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_many_through_association.rb:115:in
find_target' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/association_proxy.rb:131:inload_target’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/association_proxy.rb:122:in
method_missing' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_many_through_association.rb:108:inmethod_missing’
#{RAILS_ROOT}/app/controllers/groups_controller.rb:20:in `add’

any ideas?

Thanks!

…spike

The problem is that if you tell AR that the class name is ‘Foo’, it will
try and evaluate ‘Foo’ to get the constant. Unfortunately when you try
and eval ‘system’ then you end up calling the system function.

You class name is System (and Group in the other case), so you shouldn’t
be telling AR that the class names are system & group. Also unless
there’s other stuff you’re not showing us you don’t need class_name =>
‘system’, :foreign_key => ‘system_id’ - those are the defaults.

Fred

Well, originally I didn’t specify the class or foreign_key and I get
this NameError: uninitialized constant Group::Systems. I added those
directives when I got that errror.

so I guess you’re saying I should rename my Systems model… the only
question is what another name would be. that’s gonna require some
thought.

you think that’s what’s causing the weirdness?

Thanks!

Spike G. wrote:

Well, originally I didn’t specify the class or foreign_key and I get
this NameError: uninitialized constant Group::Systems. I added those
directives when I got that errror.

so I guess you’re saying I should rename my Systems model… the only
question is what another name would be. that’s gonna require some
thought.

you think that’s what’s causing the weirdness?

Ah, I misread slightly. Your belong_to should be belong_to :system &
belong_to :group. (not belong_to :systems). specifying the class name
would have worked if you’d supplied System instead of system.

Fred

oh man, thanks! it works!

duhh… I can’t believe I did that.

I also renamed my System model to Machine to avoid potential conflicts
later.

thanks, again!