ActiveRecord::AssociationTypeMismatch:预期用户 (#70180695416600),获得哈希 (#70180672006440)
ActiveRecord::AssociationTypeMismatch: User(#70180695416600) expected, got Hash(#70180672006440)
我正在 ROR 上试驾社交网络应用程序
我会让代码自己说话:
User.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :friendships, dependent: :destroy
has_many :inverse_friendships, class_name: "Friendship", foreign_key: "friend_id", dependent: :destroy
def request_friendship(user_2)
self.friendships.create(friend: user_2)
end
end
Friendship.rb:
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: "User"
def accept_friendship
self.update_attributes(state: "active", friended_at: Time.now)
end
def deny_friendship
self.destroy
end
def cancel_friendship
self.destroy
end
end
和我的用户模型测试..
user_spec.rb:
require 'spec_helper'
describe User, :type => :model do
let!(:user1) { User.create(email: 'test_user1@example.com', password: 'testtest', password_confirmation: 'testtest') }
let!(:user2) { User.create(email: 'test_user2@example.com', password: 'testtest', password_confirmation: 'testtest') }
it "new user has no friendships" do
expect(user1.friendships.length).to eq 0
end
it "can add another user as a friend" do
user2 = User.create(email: 'test_user2@example.com', password: 'testtest', password_confirmation: 'testtest')
user1.request_friendship(friend: user2)
expect(user1.friendships.length).to eq 1
end
end
当我 运行 这个测试时,我得到以下错误:
2) User can add another user as a friend
Failure/Error: self.friendships.create(friend: user_2)
ActiveRecord::AssociationTypeMismatch:
User(#70180695416600) expected, got Hash(#70180672006440)
# ./app/models/user.rb:11:in `request_friendship'
# ./spec/models/user_spec.rb:13:in `block (2 levels) in <top (required)>'
我希望我能提供更多细节,但代码相当基础,我只是不确定如何在我的用户模型上测试 .request_friendship 方法。
提前致谢!
user1.request_friendship(friend: user2)
在 friend: user2
的这一行中,您传递了 n 个散列,这个散列
{ :friend => user2 }
但该方法需要用户。所以
user1.request_friendship(user2)
我正在 ROR 上试驾社交网络应用程序
我会让代码自己说话:
User.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :friendships, dependent: :destroy
has_many :inverse_friendships, class_name: "Friendship", foreign_key: "friend_id", dependent: :destroy
def request_friendship(user_2)
self.friendships.create(friend: user_2)
end
end
Friendship.rb:
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: "User"
def accept_friendship
self.update_attributes(state: "active", friended_at: Time.now)
end
def deny_friendship
self.destroy
end
def cancel_friendship
self.destroy
end
end
和我的用户模型测试.. user_spec.rb:
require 'spec_helper'
describe User, :type => :model do
let!(:user1) { User.create(email: 'test_user1@example.com', password: 'testtest', password_confirmation: 'testtest') }
let!(:user2) { User.create(email: 'test_user2@example.com', password: 'testtest', password_confirmation: 'testtest') }
it "new user has no friendships" do
expect(user1.friendships.length).to eq 0
end
it "can add another user as a friend" do
user2 = User.create(email: 'test_user2@example.com', password: 'testtest', password_confirmation: 'testtest')
user1.request_friendship(friend: user2)
expect(user1.friendships.length).to eq 1
end
end
当我 运行 这个测试时,我得到以下错误:
2) User can add another user as a friend
Failure/Error: self.friendships.create(friend: user_2)
ActiveRecord::AssociationTypeMismatch:
User(#70180695416600) expected, got Hash(#70180672006440)
# ./app/models/user.rb:11:in `request_friendship'
# ./spec/models/user_spec.rb:13:in `block (2 levels) in <top (required)>'
我希望我能提供更多细节,但代码相当基础,我只是不确定如何在我的用户模型上测试 .request_friendship 方法。
提前致谢!
user1.request_friendship(friend: user2)
在 friend: user2
的这一行中,您传递了 n 个散列,这个散列
{ :friend => user2 }
但该方法需要用户。所以
user1.request_friendship(user2)