我的桌子location
:
我正在尝试在位置模型中建立关系:
belongs_to :organization, ->{Location.for_org},class_name: 'Organization',foreign_key: 'remote_id'
scope :for_org, ->{where({belongs_to: 'ORG'})}
belongs_to字段在locations表中,但是进行的查询是错误的:
ActiveRecord::StatementInvalid: Mysql2::Error:
Unknown column 'locations.belongs_to' in 'where clause':
SELECT `organizations`.* FROM `organizations` WHERE
`organizations`.`id` = 99 AND `locations`.`belongs_to` = 'ORG' LIMIT 1
仅当Location.for_org为true时,Location.organization才应返回有效记录,否则为null。
我该如何解决?
(我知道多态关系,但是它们以我不想使用的有趣方式更改字段名称)