2

I have two tables that both have no primary key. below is the two tables structure.

enter image description here

How can I use this in a join table on column date in rails ?

Meanwhile, Date columns are not unique in both tables.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Soheil
  • 4,677
  • 3
  • 21
  • 43

1 Answers1

1

From my understanding (which may be wrong), you need to use a Primary key, in order to identify foreign keys in Rails


Foreign Keys

However, you could try using the association_foreign_key and foreign_key arguments in your associations definitions (only works for HABTM):

#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

Would be so difficult to include an id column in your tables??

Richard Peck
  • 74,781
  • 9
  • 90
  • 142