I have my model defined like this:
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
and
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
Good, somewhere, I want to get the last insemination from some animal... so, I do @animal.inseminations.last
, it should work, but, it does the select using a animal_id
property, that does not exist in insemination model. So I get an error like this:
Mysql2::Error: Unknown column 'inseminations.animal_id' in 'where clause': SELECT
inseminations
.* FROMinseminations
WHEREinseminations
.animal_id
= 1 ORDER BYinseminations
.id
DESC LIMIT 1
How can I specify it to searh in cow_id
and/or bull_id
columns? Is that possible?
Thanks in advance.