Skip to content Skip to sidebar Skip to footer

Friendship Has_many Through Model With Multiple Status'

Currently my User model has the following code: has_many :notifications has_many :friendships has_many :friends, :through => :friendships, :conditions => { status: 'acc

Solution 1:

I can't find any documentation on use of :conditions with has_many, but based on the error that's being generated, I gather that the conditions specified are assumed to apply to the model that is subject of the has_many, not the target model or the model through which it is being reference.

Solution 2:

I have updated to the following and this works:

  has_many :notifications
  has_many :friendships
  has_many :accepted_friendships, :class_name => "Friendship", :conditions => {status:'accepted'}
  has_many :requested_friendships, :class_name => "Friendship", :conditions => {status:'requested'}
  has_many :pending_friendships, :class_name => "Friendship", :conditions => {status:'pending'}
  has_many :friends, :through => :accepted_friendships
  has_many :requested_friends, :through => :requested_friendships, :source => :friend
  has_many :pending_friends, :through => :pending_friendships, :source => :friend

If anyone has a different approach without having to create accepted_friendshis, requested_friendships, and pending_friendships, however, I would love to hear it!

Solution 3:

status is a column for friendships table.

So when you are writing the code, then mention the table name or else it will take the table of the Current model.

has_many :friends, -> { where  "friendships.status = 'accepted'" }, :through => :friendships

Post a Comment for "Friendship Has_many Through Model With Multiple Status'"