【发布时间】:2016-04-20 13:14:21
【问题描述】:
我知道我应该知道这一点,但我似乎根本无法弄清楚,而且我还是开发新手......
所以我有四个模型...
约会
class Appointment < ActiveRecord::Base
belongs_to :user
belongs_to :profile
belongs_to :location
end
个人资料
class Profile < ActiveRecord::Base
belongs_to :user
has_many :appointments
has_many :profile_locations
has_many :locations, through: :profile_locations
accepts_nested_attributes_for :profile_locations, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :locations, reject_if: :all_blank, allow_destroy: true
end
profile_locations
class ProfileLocation < ActiveRecord::Base
belongs_to :profile
belongs_to :location
belongs_to :location_type
accepts_nested_attributes_for :location
end
和地点
class Location < ActiveRecord::Base
has_many :profile_locations
has_many :profiles, through: :profile_locations
has_many :appointments
end
在创建约会页面上,我已经有一个相关的档案记录在案。我的 simple_form 上还有一个关联字段,用于我希望能够根据与个人资料相关的位置分配给约会的位置..
我正在尝试这样的事情,但似乎无法正常工作。
%td= f.association :location, :as => :collection_select, collection: Location.where( location.profile_location.profile_id: @profile.id ), label_method: :address_1, value_method: :id, include_blank: false, :input_html => {:class => "input-small"}, :label => "Select The Location"
我在这里遗漏了什么还是有更简单的方法来查询这个?任何有关这方面的指导都会有所帮助。
【问题讨论】:
-
你能详细解释一下吗?
标签: ruby-on-rails ruby drop-down-menu associations