ruby-on-rails – Rails多个belongs_to用于同一个类

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails多个belongs_to用于同一个类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有一个A型模型和一个B型模型,它的字段为a_id a2_id.我希望有类似的东西:
class B
  belongs_to :a
  belongs_to :a (using a2)
end
@H_301_4@有谁知道我会怎么做?我试图使用B类链接我的数据库中的类似对象.

解决方法

你可以这样做
class B
  belongs_to :a
  belongs_to :a2,foreign_key: 'a2_id',class_name: 'A'
end
原文链接:https://www.f2er.com/ruby/265342.html

猜你在找的Ruby相关文章