Writing data to join table?

I have a team-table and model, and a user-tabel and model, and a
teams_userstable for the join.
I select a few users, collect them in a group, and want to save them as
a team.
So I suppose I save one team-record, and several join-table-records,
with the same team_id, and different user_ids.

I’ve used the depot-application from Agile as a starting point,

def save_order
@group = find_group
@team = Team.new(params[:team])
@team.add_users_from_group(@group)
if @team.save
session[:group] = nil
redirect_to_index(“Bewaard”)
else
render :action => :checkout
end
end

In Team

def add_users_from_group(group)
group.items.each do |item|
li = TeamsUsers.from_group_item(item)
teams_users << li
end
end

In TeamUsers

def self.from_group_item(group_item)
li = self.new
li.team_id = group_item.id
li.user_id = group_item.id
li
end

The bit were I get an error is the line team_users << li in the
Team-bit, and I don’t know why.

I know this is a newbie question, (I am one, so…) but the point is
that it seems difficult to find clear info about that.
Could anybody please help me out? Thanks in advance.

Rudy wrote:

I have a team-table and model, and a user-tabel and model, and a
teams_users table for the join.

That’s not technically a join, it’s a many-to-many. (Technically two
joins!:wink:

Where are your has_and_belongs_to_many directives?

 teams_users << li

The bit were I get an error is the line team_users << li in the
Team-bit, and I don’t know why.

What error message? Always provide its main line, and a tiny bit of
its
stack trace.

(Don’t provide the entire stack trace, because it will provide false
hits
when anyone searches these archives for the modules it references…!)


Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!

Thanks for answering.
My has_and_belong_to many 's are in the Team and User models. Ther
error message is a “undefined local variable or method team_users” .
Could you suggest me a place or a simple example of how to get those
data in the database with a join. All the examples I find are always
about lookups, not about writing them. Probably it is so obvious that
nobody writes about it, but I’m missing it one way or the other.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs