【发布时间】:2012-09-01 14:13:17
【问题描述】:
我的模型是这样定义的:
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.* FROMinseminationsWHEREinseminations.animal_id= 1 由inseminations.idDESC 订购 限制 1 个
如何指定它在cow_id 和/或bull_id 列中搜索?这可能吗?
提前致谢。
【问题讨论】:
-
您是否尝试过使用 Bull and Cow 的实例?喜欢
@bull = Bull.first、@bull.inseminations.last。
标签: ruby-on-rails activerecord ruby-on-rails-3.2