I have two tables, media and tour_locations, related through a join
table. It’s not a pure join, because I need a sequence number on the
relationship, so the join also has an id. When I access
“aMedium.tour_locations”, the reported id’s for the TourLocation’s are
the id’s from the join table, not the real id of the tour_locations row.
The same for “aTourLocation.media”. In the sqlite3 console, the keys and
ids are all correct.
How can I fix this?
Ruby 1.8.7, Rails 2.3.5, sqlite3 3.6.12
Here is an abbreviated console dialog. MediaTourLocation(25) relates
Medium(16) to TourLocation(6).
======
tl = TourLocation.find(6)
tl.media
[… #<Medium id: 25, url: "http://www.li…> …]MediaTourLocation.find_all_by_tour_location_id(6)
[… #<MediaTourLocation id: 25, medium_sequence: 0, medium_id: 16,
tour_location_id: 6> …]med = Medium.find(16)
med.tour_locations
[#<TourLocation id: 25, locDescription: …>]
======
The Medium and the TourLocation report each other as id: 25. That’s
wrong; neither table has a row with that id. My web pages crash.
The habtm declarations and the schema appear at the end of this message.
You’ll see I’ve been thrashing
class TourLocation:
class TourLocation < ActiveRecord::Base
has_and_belongs_to_many :media,
:join_table => “media_tour_locations”,
:readonly => false
class Medium:
class Medium < ActiveRecord::Base
has_and_belongs_to_many :tour_locations,
:join_table => “media_tour_locations”,
:readonly => false
Schema:
create_table “media”, :force => true do |t|
t.string “url”, :null => false
t.string “title”, :default => “”
t.text “description”, :default => “”
t.string “media_type”, :default => “image”, :null => false
end
create_table “media_tour_locations”, :force => true do |t|
t.integer “medium_sequence”
t.integer “medium_id”
t.integer “tour_location_id”
end
create_table “tour_locations”, :force => true do |t|
t.text “locDescription”, :default => “Enter description”,
:null => false
end