我有has_many
关系:
通过历史记录has_many :polls_through_history, through: :history, class_name: "Poll", foreign_key: "poll_id", source: :poll
投票(用户)投票:
has_many
和
has_many :users_through_history, through: :history, class_name: "User", foreign_key: 'user_id', source: :user
通过历史记录投票(投票)的用户
class History < ActiveRecord::Base
belongs_to :user
belongs_to :poll
has_one :choice
end
历史模型:
require 'rails_helper'
RSpec.describe History, type: :model do
before do
@poll_owner = FactoryGirl.create(:user)
@voter = FactoryGirl.create(:user)
@poll = FactoryGirl.create(:poll, user: @poll_owner)
@choice = @poll.choices[0]
@history = FactoryGirl.create(:history, user: @voter, poll: @poll, choice: @choice)
end
subject { @history }
it { should respond_to(:user_id) }
it { should respond_to(:poll_id) }
it { should respond_to(:choice_id) }
it { should belong_to(:user) }
it { should belong_to(:poll) }
it { should have_one(:choice) }
end
历史规范:
Failure/Error: @history = FactoryGirl.create(:history, user: @voter, poll: @poll, choice: @choice)
ActiveModel::MissingAttributeError:
can't write unknown attribute `history_id`
对于所有测试用例,我总是会出现以下错误:
System.Runtime.Caching
怎么了?
提前致谢。
答案 0 :(得分:0)
根据错误消息,这是历史记录与选择模型之间关联的问题 - history_id
上没有choice
列。正如您所提到的,拥有这样一个专栏在您的案例中没有多大意义,因此您应该将has_one
更改为belongs_to
关联。