Rails active record association problem
Posted
by
Harm de Wit
on Stack Overflow
See other posts from Stack Overflow
or by Harm de Wit
Published on 2010-12-22T23:52:24Z
Indexed on
2010/12/22
23:53 UTC
Read the original article
Hit count: 146
Hello,
I'm new at active record association in rails so i don't know how to solve the following problem:
I have a tables called 'meetings' and 'users'. I have correctly associated these two together by making a table 'participants' and set the following association statements:
class Meeting < ActiveRecord::Base
has_many :participants, :dependent => :destroy
has_many :users, :through => :participants
and
class Participant < ActiveRecord::Base
belongs_to :meeting
belongs_to :user
and the last model
class User < ActiveRecord::Base
has_many :participants, :dependent => :destroy
At this point all is going well and i can access the user values of attending participants of a specific meeting by calling @meeting.users in the normal meeting>show.html.erb view.
Now i want to make connections between these participants. Therefore i made a model called 'connections' and created the columns of 'meeting_id', 'user_id' and 'connected_user_id'. So these connections are kinda like friendships within a certain meeting.
My question is: How can i set the model associations so i can easily control these connections?
I would like to see a solution where i could use
@meeting.users.each do |user|
user.connections.each do |c|
<do something>
end
end
I tried this by changing the model of meetings to this:
class Meeting < ActiveRecord::Base
has_many :participants, :dependent => :destroy
has_many :users, :through => :participants
has_many :connections, :dependent => :destroy
has_many :participating_user_connections, :through => :connections, :source => :user
Please, does anyone have a solution/tip how to solve this the rails way?
© Stack Overflow or respective owner