Michael Barinek wrote:> Dr Nic wrote: >> >> Your memberships table would need a boolean ''captain'' field, which is >> set to true for the user,club combination as appropriate > > > is memberships similar to teams_users?Yep. Same columns (plus the captain column), with a better name. -- Posted via http://www.ruby-forum.com/.
the below...
class User < ActiveRecord::Base
has_and_belongs_to_many :teams
class Team < ActiveRecord::Base
has_and_belongs_to_many :users
has_one :captain, :class_name => ''User''
produces the error...
Mysql::Error: Unknown column ''users.team_id'' in
''where clause'': SELECT *
FROM users WHERE (users.team_id = 1) LIMIT 1
i realize the error - and my current solution is to add user_id to
team...
then i have...
class Team < ActiveRecord::Base
has_and_belongs_to_many :users
def captain
User.find(user_id)
end
although the above solution seems awkward - is there a better way to
accomplish the above?
--
Posted via http://www.ruby-forum.com/.
class User < ActiveRecord::Base
has_many :memberships
has_many :teams, :through => :memberships
class Team < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
has_one :captain, :class_name => ''User'', :conditions
=>
[''memberships.captain = ?'', true], :through =>
:memberships
end
class Membership < ActiveRecord::Base
end
I haven''t run this, but it looks nice.
--
Posted via http://www.ruby-forum.com/.
Your memberships table would need a boolean ''captain'' field, which is set to true for the user,club combination as appropriate -- Posted via http://www.ruby-forum.com/.
Dr Nic wrote:> > Your memberships table would need a boolean ''captain'' field, which is > set to true for the user,club combination as appropriateis memberships similar to teams_users? -- Posted via http://www.ruby-forum.com/.
last question... so i still have a problem with select boxes and the controller... class Team < ActiveRecord::Base has_many :memberships has_many :users, :through => :memberships class User < ActiveRecord::Base has_many :memberships has_many :teams, :through => :memberships class Membership < ActiveRecord::Base belongs_to :user belongs_to :team def create @team = Team.new(params[:team]) NoMethodError in TeamController#create undefined method `user_ids='' for #<Team:0x2789500> so i looked - user_ids isn''t there - just membership_ids... -- Posted via http://www.ruby-forum.com/.
i solved the above using the below...
def create
@team = Team.new(params[:team])
if @team.save
flash[:notice] = ''Team was successfully created.''
Membership.new(:user => @session[''user''], :team =>
@team,
:is_the_captain => true).save
User.find(params[:membership][:user_ids]).each do |user|
Membership.new(:user => user, :team => @team).save
end
still seems strange...
thoughts/comments?
--
Posted via http://www.ruby-forum.com/.
User.find(params[:membership][:user_ids]).each do |user|
Membership.new(:user => user, :team => @team).save
end
I don''t get why you''re doing this. If you''re creating
a new Team, then
the creator will be the first and only member at that time.
--
Posted via http://www.ruby-forum.com/.
what if they don''t select their-self?
here is the view...
<% @selected = @team.users.collect { |v| v.id.to_i } %>
<select name="membership[user_ids][]"
multiple="multiple">
<%= options_from_collection_for_select(@users, "id",
"login",
@selected) %>
and the db...
create table memberships (
id int(10) unsigned not null auto_increment,
team_id int(10) unsigned not null,
user_id int(10) unsigned not null,
is_the_captain tinyint(1) not null default ''0'',
primary key (id),
unique (team_id, user_id),
foreign key (team_id) references teams(id),
foreign key (user_id) references users(id)
) engine=innodb default charset=latin1;
--
Posted via http://www.ruby-forum.com/.