【问题标题】:Last with a custom column name最后带有自定义列名
【发布时间】: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.* FROM inseminations WHERE inseminations.animal_id = 1 由inseminations.id DESC 订购 限制 1 个

如何指定它在cow_id 和/或bull_id 列中搜索?这可能吗?

提前致谢。

【问题讨论】:

  • 您是否尝试过使用 Bull and Cow 的实例?喜欢@bull = Bull.first@bull.inseminations.last

标签: ruby-on-rails activerecord ruby-on-rails-3.2


【解决方案1】:

我认为您有几个不同的选择:

1) 不使用has_many :inseminations,而是创建两个独立的有很多关系:

has_many :cow_inseminations, :class_name => 'Insemination', :foreign_key => 'cow_id'
has_many :bull_inseminations, :class_name => 'Insemination', :foreign_key => 'bull_id'

2) 使用 STI 并创建 Animal 的子类。您需要向 Animal 添加一个类型字段才能使其工作:

class Cow < Animal
  has_many :inseminations, :foreign_key => 'cow_id'
end

class Bull < Animal
  has_many :inseminations, :foreign_key => 'bull_id'
end

然后你可以做 Bull.first.inseminations 或 Cow.first.inseminations

【讨论】:

    【解决方案2】:

    你可以指定一个外键:

    has_many :inseminations, :foreign_key => :bull_id
    

    但是你只能有一个foreign_key,所以它对奶牛不起作用。

    您可以执行Rails Model has_many with multiple foreign_keys 之类的操作来使其正常工作。但是在这种情况下,您需要:

    has_many :bull_inseminations, :foreign_key => :bull_id
    has_many :cow_inseminations, :foreign_key => :cow_id
    
    def inseminations
      # not sure how you store animal type, but something like
      return bull_inseminations if animal_type == "bull"
      return cow_inseminations if animal_type == "cow"
    end
    

    对于其他属性方法,如果你想使用它们,你需要做类似的事情,例如:

    def inseminations_changed?
      bull_inseminations_changed? or cow_inseminations_changed?
    end
    

    与人工授精类似

    【讨论】:

    • 谢谢...我首先测试了Beerlington,因为它看起来更简单,但我相信你也应该工作。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2019-11-06
    • 2017-02-06
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    相关资源
    最近更新 更多