我的模型定义如下:
class Animal < ActiveRecord::Base
mount_uploader :picture, PictureUploader
attr_accessible :picture, :born_date, :father_id, :mother_id, :name, :obs, :earring, :animal_type, :animal_type_id, :inseminations
validates :name, :born_date, :presence => true
validates :earring, :presence => true, :if => :should_have_earring?
belongs_to :father, :class_name => "Animal"
belongs_to :mother, :class_name => "Animal"
belongs_to :animal_type
has_one :birth
has_one :sell
has_one :death
has_many :inseminations
end
和
class Insemination < ActiveRecord::Base
attr_accessible :bull_id, :cow_id, :done, :expected_birth_date, :expected_dry_date, :insemination_date, :obs, :rut
validates :bull_id, :presence => true
validates :cow_id, :presence => true
validates :insemination_date, :presence => true
belongs_to :bull, :class_name => "Animal"
belongs_to :cow, :class_name => "Animal"
has_one :birth
has_one :abortion
has_one :dry
end
好,在某个地方,我想从某种动物身上进行最后一次授精……所以,我这样做@animal.inseminations.last
了,它应该可以工作,但是,它使用animal_id
授精模型中不存在的属性进行选择。所以我收到这样的错误:
Mysql2::Error:'where 子句'中的未知列'inseminations.animal_id':SELECT
inseminations
.* FROMinseminations
WHEREinseminations
。animal_id
= 1 订购方式inseminations
。id
DESC 限制 1
如何指定它来搜索cow_id
和/或bull_id
列?那可能吗?
提前致谢。