jon*_*oun 6 ruby ruby-on-rails rails-models ruby-on-rails-3
假设我有一个A型模型和一个B型模型,它的字段为a_id a2_id.我希望有类似的东西:
class B
belongs_to :a
belongs_to :a (using a2)
end
Run Code Online (Sandbox Code Playgroud)
有谁知道我会怎么做?我试图使用B类链接我的数据库中的类似对象.
你可以这样做
class B
belongs_to :a
belongs_to :a2, foreign_key: 'a2_id', class_name: 'A'
end
Run Code Online (Sandbox Code Playgroud)