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)
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.
Can you post your migrations for what the tables look like in the db?
Those classes look ok, so I’m wondering if you have your table defs set
up right.
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 tried that out (on edge) and it works fine for me. What version of
rails are you running?
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… is there a good way to figure
out where Rails is getting this expectation for an argument?
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)
)