Ruby on rails Rails 4多对多关联
我试图建立一个Ruby on rails Rails 4多对多关联,ruby-on-rails,ruby,activerecord,many-to-many,associations,Ruby On Rails,Ruby,Activerecord,Many To Many,Associations,我试图建立一个有很多:通过三个模型之间的关联:旅行,位置和位置带,但这些关联似乎没有相互联系 我成立了我的协会: class Trip < ActiveRecord::Base belongs_to :user has_many :locations_trips has_many :locations, :through => :locations_trips accepts_nested_attributes_for :locations_trips, :allo
有很多:通过三个模型之间的关联:旅行,位置和位置带,但这些关联似乎没有相互联系
我成立了我的协会:
class Trip < ActiveRecord::Base
belongs_to :user
has_many :locations_trips
has_many :locations, :through => :locations_trips
accepts_nested_attributes_for :locations_trips, :allow_destroy => :true
accepts_nested_attributes_for :locations, :allow_destroy => :true
end
class Location < ActiveRecord::Base
has_many :locations_trips, :dependent => :destroy
has_many :trips, :through => :locations_trips
end
class LocationsTrip < ActiveRecord::Base
belongs_to :locations
belongs_to :trips
end
所以我显然错过了一些关键的东西,但我觉得好像我已经看过这里的每一个类似的答案,看不出我错在哪里
可能值得一提的是,我正在尝试设置Trips
接受位置的嵌套属性,因此在Trip
表单部分中,我得到了一个f.fields\u,用于:位置做|生成器|
等,但当我加载页面时,我得到的名称错误与以前相同
如果有人能给我指出正确的方向,我将万分感激——我觉得我在这件事上获得了一种隧道式的远见
我使用的是Ruby 2.1.2和Rails 4.2.0。看起来您使用的是属于
关联的联接表的复数名称。尝试更改为:
class LocationsTrip < ActiveRecord::Base
belongs_to :location
belongs_to :trip
end
class LocationsTrip
你能显示你的应用/应用/模型的树的输出吗?
class LocationsTrip < ActiveRecord::Base
belongs_to :location
belongs_to :trip
end