【发布时间】:2014-07-04 02:29:53
【问题描述】:
我有一个模型,我希望列包含不同的 ID。所以一个用户可以关注另一个用户,但不能关注他们自己。
迁移
class CreateFollows < ActiveRecord::Migration
def change
create_table :follows do |t|
t.integer :follower_id
t.integer :followee_id
t.timestamps
end
end
end
型号
class Follow < ActiveRecord::Base
belongs_to :follower, class_name: 'User'
belongs_to :followee, class_name: 'User'
validates :follower_id, uniqueness: { scope: :followee_id }
end
但我的测试似乎失败了
测试
it 'cannot follow itself' do
user = FactoryGirl.create(:user)
follow = FactoryGirl.create(:follow, follower: user, followee: user)
expect(follow).to be_invalid
end
输出
Failures:
1) Follow cannot follow itself
Failure/Error: expect(follow).to be_invalid
expected `#<Follow id: 27, follower_id: 78, followee_id: 78, created_at: "2014-07-04 02:20:59", updated_at: "2014-07-04 02:20:59">.invalid?` to return true, got false
# ./spec/models/follow_spec.rb:23:in `block (2 levels) in <top (required)>'
从我读过的所有内容来看,这看起来是写的。有人有任何指示吗?
谢谢
【问题讨论】:
标签: ruby-on-rails ruby validation activerecord models