【发布时间】:2017-04-24 19:06:24
【问题描述】:
我有一个非常具体的问题,所以我会把它翻译成更容易理解和更清晰的例子。
所以我们有“Country”和“Image”模型。一个国家有它的旗帜和它的手臂。
这意味着我们必须将 Country 与 Image 连接 2 次。我尝试转换 Rails 指南的配方“连接到自身”,但是,我总是遇到异常:“预期图像,得到字符串”。
*Country model
class Country < ApplicationRecord
has_one :flag, class_name: 'Image', foreign_key: 'id'
has_one :arm, class_name: 'Image', foreign_key: 'id'
end
*Image model
class Image < ApplicationRecord
belongs_to :flag, class_name: 'Country'
belongs_to :arm, class_name: 'Country'
end
【问题讨论】:
标签: ruby-on-rails model-view-controller