has_many through condition和state_machines

时间:2016-08-25 08:47:17

标签: ruby-on-rails ruby ruby-on-rails-4 state-machine

我正在使用TreeHouse上的小脸谱应用程序(他们使用rails 3 ans我在轨道4上......)我和Unknown key: :conditions面对同样的探测器,我发现了更多信息{{3} }。

我的所有测试都在通过,直到我不得不更新用户模型 从这个:

#1 user.rb

  has_many :user_friendships
  has_many :friends, through: :user_friendships

到此:

#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

不幸的是,这并没有解决我的问题....我做错了什么? here有人建议写{where {state:pending“}}但我有语法错误......所以我写的方式似乎被接受了......

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

关于测试:

它适用于该模型的第一个版本。 然后当我切换到模型的“新”版本时,我必须改变这一行:

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

进入这个

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

这是我的全部测试

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

这些是我的失败......

  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

希望你能在这里帮助我!非常感谢

1 个答案:

答案 0 :(得分:0)

其实我只使用:

gem 'state_machines'

必须有另一个宝石:

gem 'state_machines'
gem 'state_machines-activerecord'