我有一个多态Transaction模型from_owner,因为事务可能来自其他几个模型。
class Transaction < ActiveRecord::Base
belongs_to :from_owner, polymorphic: true
end
我正在尝试belongs_to为何时from_owner_type是特定值设置一个特定的值:
belongs_to :from_person,
conditions: ['from_owner_type = ?', Person.name],
class_name: Person,
foreign_key: 'from_owner_id'
我遇到的问题是conditions似乎是 forPerson而不是Transaction. 所以我在尝试调用from_persona 时收到以下 SQL 错误Transaction:
ActiveRecord::StatementInvalid: SQLite3::SQLException: 没有这样的列: from_owner_type: SELECT "people".* FROM "people" WHERE "people"."id" = 1 AND (from_owner_type = 'Person') LIMIT 1
我想要的是from_personon a如果不是则Transaction返回,否则返回相关的。我可以设置一个自定义方法来执行此操作,但我认为它可能作为. 我想在 CanCan 条件下使用它。我正在使用 Rails 3。nilTransaction from_owner_typePersonPersonfrom_personbelongs_to