我有两个没有主键的表。下面是两个表的结构。
我如何在 rails 中的列日期连接表中使用它?
同时,日期列在两个表中都不是唯一的。
根据我的理解(可能是错误的),您需要使用主键来识别 Rails 中的外键
外键
但是,您可以尝试在关联定义 (only works for HABTM) 中使用 association_foreign_key
和 foreign_key
参数:
#app/models/table_a.rb
Class TableA < ActiveRecord::Base
has_and_belongs_to_many :table_b, foreign_key: "country_id", association_foreign_key: "hour"
end
#app/models/table_b.rb
Class TableA < ActiveRecord::Base
has_and_belongs_to_many :table_a, foreign_key: "hour", association_foreign_key: "country_id"
end
table_as_table_bs
hour | country_id
在您的表格中包含一个 id
列会这么难吗??
我是一名优秀的程序员,十分优秀!