0

ArgumentError: Unknown key: :conditions. Valid keys are: :class_name, :class, :foreign_key

First of all, there is a question, which is almost similar to mine, but I cannot make it work for my code. So, I decided to ask separately.

This is where I get error:

  has_many :friendships
  has_many :friends,
           :through => :friendships,
           :conditions => "status = 'accepted'",
           :order => :screen_name
  has_many :requested_friends,
           :through => :friendships,
           :source => :friend,
           :conditions => "status = 'requested'",
           :order => :created_at
  has_many :pending_friends,
           :through => :friendships,
           :source => :friend,
           :conditions => "status = 'pending'",
           :order => :created_at

I don't know if I should share some other of my codes to help you to understand. If you need some other parts, I can also paste them.

This is the full error:

Unknown key: :conditions. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :table_name, :before_add, :after_add, :before_remove, :after_remove, :extend, :primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache, :join_table, :foreign_type

Thank you.

EDIT

I fixed the problem:

  has_many :friendships
  has_many :friends, -> { where(friendship: {status: 'accepted'}).order('created_at') }, :through => :friendships
  has_many :requested_friends, -> { where(friendship: {status: 'requested'}).order('created_at') }, :through => :friendships, :source => :friend
  has_many :pending_friends, -> { where(friendship: {status: 'pending'}).order('created_at') }, :through => :friendships, :source => :friend

This is the error I am getting now:

Mysql2::Error: Unknown column 'friendship.status' in 'where clause': SELECT 1 AS one FROM `users` INNER JOIN `friendships` ON `users`.`id` = `friendships`.`friend_id` WHERE `friendships`.`user_id` = 6 AND `friendship`.`status` = 'requested' AND `users`.`id` = 8 LIMIT 1

This is where error occurs:

  def accept # accept_request
    if @user.requested_friends.include?(@friend)
      Friendship.accept_request(@user, @friend)
    end
    redirect_to profile_path(params[:id])
  end

Specifically here:

if @user.requested_friends.include?(@friend)

accept_request:

  def self.accept_request(user, friend)
    transaction do
      accept_one_side(user, friend)
      accept_one_side(friend, user)
    end
  end

accept_one_side:

  private
  def self.accept_one_side(user, friend)
    request = find_by_user_id_and_friend_id(user, friend)
    request.status = 'accepted'
    request.save!
  end
Community
  • 1
  • 1
cyonder
  • 852
  • 1
  • 15
  • 36
  • possible duplicate of [What is the equivalent of conditions in has\_many Rails 4](http://stackoverflow.com/questions/20307874/what-is-the-equivalent-of-conditions-in-has-many-rails-4) – Brad Werth Sep 25 '15 at 22:11
  • According to the link you gave me, I fixed the problem. But now i am getting some other error. – cyonder Sep 25 '15 at 22:41
  • It looks like you are trying to call a column from an associated model, which requires a join. Can you post the code where the error is occurring? – Ryan K Sep 25 '15 at 22:52
  • @RyanK probably error is in has_many but still cannot figure out – cyonder Sep 25 '15 at 23:14
  • What you need is a [join](http://apidock.com/rails/ActiveRecord/QueryMethods/joins). Probably something like: `joins(:friendships).where(friendship: {status: 'accepted'}}`. Notice the singular friendship in the where clause. – Ryan K Sep 25 '15 at 23:29
  • Cool. Answer your own question since you fixed it. Might help someone else later. – Ryan K Sep 25 '15 at 23:39

1 Answers1

0

Problem Fixed

Problem was in has_many.

This is the correct version:

  has_many :friendships
  has_many :friends, -> { where(friendships: {status: 'accepted'}).order('created_at') }, :through => :friendships
  has_many :requested_friends, -> { where(friendships: {status: 'requested'}).order('created_at') }, :through => :friendships, :source => :friend
  has_many :pending_friends, -> { where(friendships: {status: 'pending'}).order('created_at') }, :through => :friendships, :source => :friend

It may look like same code above but its not.

Because in first code, I wrote: "where(friendship:" but it supposed to be: "where(friendships:" So, I forgot to put 's'

cyonder
  • 852
  • 1
  • 15
  • 36