associations - Has many through relationship in rails -
associations - Has many through relationship in rails -
i using facebook api fetch users fb.
i want store users in model user
i using has many through
relationship store users
user model relationship have in user
model.
has_many :friends, :through => :user_friends, :class_name => "user", :foreign_key => "friend_id"
user friends
model intermediate table fetch friends of user.
belongs_to :user belongs_to :user, :foreign_key => "friend_id"
user friends has user_id
, friend_id
columns added in migration.
i error when utilize .friends
on user object.
activerecord::hasmanythroughassociationnotfounderror: not find association :user_friends in model user
can help this? in advance.
you need check self-referential association. apparently missing concepts. can not add together 2 associations same name in single model, (only 1 of them respond).
you should add together has_many :user_friends
, still missing other side of association, check example:
# user.rb has_many :user_friends has_many :friends, :through => :user_friends has_many :inverse_friendships, :class_name => "userfriend", :foreign_key => "friend_id" has_many :inverse_friends, :through => :inverse_friendships, :source => :user
# user_friends.rb belongs_to :user belongs_to :friend, :class_name => "user"
ruby-on-rails associations has-many model-associations
Comments
Post a Comment