我有一个属于 2 个项目实例的 ActiveRecord 模型“报价”。项目属于用户。
报价模型具有以下内容:
提供_item_id 接收_item_id 提供_user_id 接收_user_id
在创建模型时,我希望 Rails 通过读取项目 ID 并遍历该关系来保存用户。
我有这个:
class Offer < ActiveRecord::Base
attr_accessible :offered_item_id, :receiving_item_id, :state
belongs_to :offering_user, class_name: "User"
belongs_to :receiving_user, class_name: "User"
belongs_to :offered_item, class_name: "Item"
belongs_to :receiving_item, class_name: "Item"
before_save :populate_user_ids
private
def populate_user_ids
self.receiving_user_id = self.receiving_item.user.id
self.offering_user_id = self.offered_item.user.id
end
end
但即使对populate_user_ids
方法中的值进行硬编码似乎也不起作用。我before_save
什至不确定是否被称为。
我的测试通过了项目关系,但用户关系惨遭失败。
查看我的 RSpec 测试:
describe "relations" do
it { should respond_to (:offering_user) }
it { should respond_to (:offered_item) }
it { should respond_to (:receiving_user) }
it { should respond_to (:receiving_item) }
its(:offering_user) { should == offering_user } #FAILS
its(:offered_item) { should == offered_item }
its(:receiving_user) { should == receiving_user } #FAILS
its(:receiving_item) { should == receiving_item }
end
抱歉,我对 Rails 很陌生。
可能是什么问题呢?
谢谢