これら 2 つのエラーが発生しましたが、修正方法がわかりません。私が直面している問題の 1 つは、チュートリアルの新しいバージョンと古いバージョン (Rails 4 と 3.2 の違い) の違いです。
私の仕様は次のとおりです。
ルビーのバージョン: 1.9.2p320
レールのバージョン: 3.2.13
Rスペック: 2.11.1
コンピューター: Macbook Pro OS X Mountain Lion
エラー
1) User following and unfollowing
Failure/Error: before { @user.unfollow!(other_user) }
NoMethodError:
undefined method `find_by' for []:ActiveRecord::Relation
# ./app/models/user.rb:36:in `unfollow!'
# ./spec/models/user_spec.rb:47:in `block (4 levels) in <top (required)>'
2) User following and unfollowing followed_users
Failure/Error: before { @user.unfollow!(other_user) }
NoMethodError:
undefined method `find_by' for []:ActiveRecord::Relation
# ./app/models/user.rb:36:in `unfollow!'
# ./spec/models/user_spec.rb:47:in `block (4 levels) in <top (required)>'
ユーザー.rb
def following?(other_user)
relationships.where(followed_id: other_user.id).first
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by(followed_id: other_user.id).destroy!
end
user_spec.rb
describe "following" do
let(:other_user) { FactoryGirl.create(:user) }
before do
@user.save
@user.follow!(other_user)
end
it { should be_following(other_user) }
its(:followed_users) { should include(other_user) }
describe "followed users" do
subject { other_user }
its(:followers) {should include(@user) }
end
describe "and unfollowing" do
before { @user.unfollow!(other_user) }
it {should_not be_following(other_user) }
its(:followed_users) {should_not include(other_user) }
end
end