# ArgumentError in has\_many :through

**URL:** <https://www.ruby-forum.com/t/argumenterror-in-has-many-through/115265>\
**Category:** Rails\
**Created:** [September 22, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/argumenterror-in-has-many-through/115265 "2007-09-22T22:36:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 22, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/argumenterror-in-has-many-through/115265/1 "2007-09-22T22:36:41Z")

</div>

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:in`compute\_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:in`klass’  
/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:in`load\_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:in`method\_missing’  
#{RAILS\_ROOT}/app/controllers/groups\_controller.rb:20:in `add’

any ideas?

Thanks!

…spike

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 22, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/argumenterror-in-has-many-through/115265/2 "2007-09-22T22:45:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 22, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/argumenterror-in-has-many-through/115265/3 "2007-09-22T23:14:38Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 23, 2007, 12:46am UTC](https://www.ruby-forum.com/t/argumenterror-in-has-many-through/115265/4 "2007-09-23T00:46:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 23, 2007, 1:06am UTC](https://www.ruby-forum.com/t/argumenterror-in-has-many-through/115265/5 "2007-09-23T01:06:35Z")

</div>

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!
