0

I am doing the little facebook app on TreeHouse (they use rails 3 ans I am on rails 4...) and I was facing the same probelem as here Unknown key: :conditions, I found more info here.

All my test were passing until I had to update the user model from this:

#1 user.rb

  has_many :user_friendships
  has_many :friends, through: :user_friendships

to this:

#2 user.rb

has_many :user_friendships
  has_many :friends,-> { where(user_friendships: { state: "accepted"}) }, through: :user_friendships
  has_many :pending_user_friendships, -> { where  state: "pending"  }, class_name: 'UserFriendship', foreign_key: :user_id
  has_many :pending_friends, through: :pending_user_friendships, source: :friend

unfortunately this doesn't solve my problem .... what I did wrong? here it was suggested to write {where{state:pending"}} but I had a syntax error... so the way I wrote seems to be accepted...

user_friendship.rb

class UserFriendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name: 'User', foreign_key: "friend_id"

  state_machine :state, initial: :pending do
    after_transition on: :accept, do: :send_acceptance_email

    state :requested

    event :accept do
      transition any => :accepted
    end
  end

  def self.request(user1,user2)
    transaction do
      friendship1 = create!(user: user1, friend: user2, state: "pending")
      friendship2 = create!(user: user2, friend: user1, state: "requested")

      friendship1.send_request_email
      friendship1
    end
  end

  def send_request_email
    UserNotifier.friend_requested(id).deliver_now
  end

  def send_acceptance_email
     UserNotifier.friend_request_accepted(id).deliver_now
  end
end

About the test:

It works fine with the first version of the model. And then when I switch to the "new" verson of the model I have to change this line:

assert users(:jason).friends.include?(users(:mike))

into this

assert users(:jason).pending_friends.include?(users(:mike))

this is my whole test

require 'test_helper'

class UserFriendshipTest < ActiveSupport::TestCase
  should belong_to(:user)
  should belong_to(:friend)

  test "that creating a friendship works without raising an exception" do
    assert_nothing_raised do
      UserFriendship.create user: users(:jason), friend: users(:mike)
    end
  end

  test "that creating a friendship based on user id and friend id works" do
    UserFriendship.create user_id: users(:jason).id, friend_id: users(:mike).id
    assert users(:jason).friends.include?(users(:mike))
    #assert users(:jason).pending_friends.include?(users(:mike))

  end

  context "a new instance" do
    setup do
      @user_friendship = UserFriendship.new user: users(:jason), friend: users(:mike)
    end

    should "have a pending state" do
      assert_equal 'pending', @user_friendship.state
    end
  end

  context "#send_request_email" do
    setup do
      @user_friendship = UserFriendship.create user: users(:jason), friend: users(:mike)
    end
    should "send an email" do
      assert_difference 'ActionMailer::Base.deliveries.size', 1 do
        @user_friendship.send_request_email
      end
    end
  end

  context "#accept!" do
    setup do
      @user_friendship = UserFriendship.create user: users(:jason), friend: users(:mike)
    end

    should "set the state to accepted" do
      @user_friendship.accept!
      assert_equal "accepted", @user_friendship.state
    end

    should "send an acceptance email" do
      assert_difference 'ActionMailer::Base.deliveries.size', 1 do
        @user_friendship.accept!
      end
    end

    should "include the friend in the list of friends" do
      @user_friendship.accept!
      users(:jason).friends.reload
      assert users(:jason).friends.include?(users(:mike))
    end
  end

  context".request" do
    should "Create two user friendships" do
      assert_difference 'UserFriendship.count', 2 do
        UserFriendship.request(users(:jason),users(:mike))
      end
    end

    should "send a friend request email" do
      assert_difference 'ActionMailer::Base.deliveries.size', 1 do
        UserFriendship.request(users(:jason),users(:mike))
      end
    end
  end
end

These are my failures...

  1) Failure:
UserFriendshipTest#test_: #accept! should include the friend in the list of friends.  [test/models/user_friendship_test.rb:60]:
Expected false to be truthy.


  2) Failure:
UserFriendshipTest#test_that_creating_a_friendship_based_on_user_id_and_friend_id_works [test/models/user_friendship_test.rb:16]:
Expected false to be truthy.

11 runs, 10 assertions, 2 failures, 0 errors, 0 skips

Hope you could help me here ! thanks a lot

Community
  • 1
  • 1

1 Answers1

0

Actually I was using only:

gem 'state_machines'

It has to come with another gem:

gem 'state_machines'
gem 'state_machines-activerecord'