has_many through Problem...

I've searched the message boards and the web, and I can't figure out why I get a weird '# of arguments' error... :-\ Thanks in advance for any help.

I have three models:

class User < ActiveRecord::Base   has_many :memberships   has_many :groups, :through => :memberships end

class Group < ActiveRecord::Base   has_many :memberships   has_many :users, :through => :memberships end

class Membership < ActiveRecord::Base   belongs_to :group   belongs_to :user end

I have a user stored in @user,and I want a list of the groups, so I write: @user.group

and get this error: wrong number of arguments (0 for 1)

I can reference @user.memberships.find(:first) and it works fine. I can even reference @user.memberships.find(:first).some_random_attribute and it works, too. but if I try @user.memberships.find(:first).group it barfs with the same error!

I have also tried (for experimentation): @user.group(1)

and I still get the error: wrong number of arguments (0 for 1)

Any ideas??

here's the application trace:

e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:1360:in `initialize' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:1360:in `open' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:1360:in `compute_type' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:1066:in `instantiate_without_callbacks' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ callbacks.rb:204:in `instantiate' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:427:in `find_by_sql' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:427:in `collect!' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:427:in `find_by_sql' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:997:in `find_every' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:418:in `find' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations/has_many_through_association.rb:115:in `find_target' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations/association_proxy.rb:131:in `load_target' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations/association_proxy.rb:55:in `reload' e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations.rb:938:in `groups' #{RAILS_ROOT}/app/views/main/welcome.rhtml:24:in `_run_rhtml_47app47views47main47welcome46rhtml'

I should add that I can call: @user.memberships.find(:first).user

and get back to the original user (same as @user). for some reason, though, attempting to follow the "belongs_to group" link creates the error.

edit:

the code I pasted in my first message was incorrect... trying @user.group of course generates a "method not defined error"

I was in fact, using @user.groups which generates the "wrong num of args" error.

@user.groups(1) generates the same error.

sorry for the confusion.

[[continuing the conversation with myself...]]

The error has nothing to do with :through...

given the models:

class User < ActiveRecord::Base   has_many :memberships end

class Group < ActiveRecord::Base   has_many :memberships end

class Membership < ActiveRecord::Base   belongs_to :group   belongs_to :user end

the code: @user.memberships.find(:first).group creates the "too few args" error.

I set up the DBs directly through phpMyAdmin. Here's the structure i get when I export them...

CREATE TABLE `groups` (   `id` int(11) NOT NULL auto_increment,   `name` varchar(100) collate latin1_general_ci NOT NULL,   `short_name` varchar(100) collate latin1_general_ci NOT NULL,   `type` varchar(100) collate latin1_general_ci NOT NULL default 'open'   PRIMARY KEY (`id`) )

CREATE TABLE `users` (   `id` int(11) NOT NULL auto_increment,   `name` varchar(100) collate latin1_general_ci default NULL,   `phone_num` bigint(15) NOT NULL,   `email` varchar(40) collate latin1_general_ci default NULL,   `password` varchar(50) collate latin1_general_ci default NULL,   PRIMARY KEY (`id`) )

CREATE TABLE `memberships` (   `id` int(11) NOT NULL auto_increment,   `user_id` int(11) NOT NULL,   `group_id` int(11) NOT NULL,   `member_type` int(11) NOT NULL default '0' COMMENT 'regular users are type ''0'', moderators and owners get a higher type',   `user_status` int(11) NOT NULL default '1' COMMENT '0 is offline; 1 is online',   PRIMARY KEY (`id`) )

I just tried re-installing Ruby, Rails and XAMPP on a different computer, copying over only the /app folder from my previous try...

here's the install stats: Ruby version 1.8.6 (i386-mswin32) RubyGems version 0.9.5 Rails version 1.2.5 Active Record version 1.15.5 Action Pack version 1.13.5 Action Web Service version 1.2.5 Action Mailer version 1.3.5 Active Support version 1.4.4

I'm getting the same odd error... :frowning: is there a good way to figure out where Rails is getting this expectation for an argument?

I've narrowed the error down further.

I went into the Rails console and tried:

Group.find(1)

Error: wrong number of arguments 0 for 1 (!!)

and, of course: Group.find() Error: Couldn't find group without ID

Any idea what could prevent the ActiveRecord from getting set up correctly?

Okay... problem solved... I had a column in the Groups table called "type" which is a magic field table.

*DOH!*

Thanks for you help, Josh.