假設我有以下型號的現有應用:的Rails:轉換的has_many到HAS_ONE協會
class User < ActiveRecord::Base
has_many :locations, :through => :location_users
end
class Location < ActiveRecord::Base
has_many :users, :through => :location_users
end
我如何去了解這個的has_many轉換爲HAS_ONE協會像下面,就遷移,修剪記錄有多個地點的人,還有我錯過的其他東西?有沒有任何捷徑來做這種轉換?
class User < ActiveRecord::Base
belongs_to :location
end
class Location < ActiveRecord::Base
has_many :users
end
編輯:用戶屬於一個且只有一個位置
你確定你想讓位置表存儲'user_id'的主鍵嗎?或者應該是'users'表存儲一個'location_id'(即用戶將只屬於一個位置)? –
讓用戶belongs_to更合理:位置和位置has_many:用戶? ...是的,carlosramireziii說了些什麼。 – Coenwulf
你們都對,這是我的邏輯錯誤。我現在編輯它。 – konyak