Ruby on rails Rails中连接模型表单的多个字段

Ruby on rails Rails中连接模型表单的多个字段,ruby-on-rails,has-many-through,fields-for,Ruby On Rails,Has Many Through,Fields For,我有3个模型,公寓,房屋规则和公寓规则关系,这是一个连接模型,有很多:通过关系 class Apartment < ApplicationRecord has_many :apartment_rule_relations has_many :house_rules, through: :apartment_rule_relations accepts_nested_attributes_for :house_rules accepts_nested_attributes_

我有3个模型,公寓,房屋规则和公寓规则关系,这是一个连接模型,有很多:通过关系

class Apartment < ApplicationRecord
  has_many :apartment_rule_relations 
  has_many :house_rules, through: :apartment_rule_relations
  accepts_nested_attributes_for :house_rules
  accepts_nested_attributes_for :apartment_rule_relations
班级公寓
class houserrule
class ApartmentRuleRelation
在创建任何公寓实例时,将创建与所有和任何HouseRules的关系。我的连接模型有一个布尔属性:state,我最终希望将其用于SQL查询。在创建单元的表单中,我希望列出所有单元关系,并允许最终用户选择:state属性(对于单元中允许的规则为true,对于不允许的规则为false,对于未选择的规则为默认nil状态)。表单字段_的数量由内部规则的数量决定

我似乎不明白应该如何格式化此类操作的参数列表,也不知道如何为每个规则的表单正确地呈现多个字段

如果有什么帮助的话,我试过这样做,但是没有用

form.html.erb

<%= form_for [:landlord, :profile, @apartment] do |form| %>
  <% HouseRule.all.each do |rule| %>
    <%= form.fields_for :apartment_rule_relations do |ar_form| %>
       <%= ar_form.radio_button(:state, true) %>
       <%= ar_form.radio_button(:state, false) %>
       <%= ar_form.hidden_field(house_rule_id: rule.id) %>
    <% end %>
  <% end %>
<% end %>

___________________________________________________________

apartments_controller.rb

params.require(:apartment).permit({ apartment_rules_relation_attributes: [:house_rule_id, :state, house_rules_attributes: [:apartment_id] ] }

form.html.erb
___________________________________________________________
rb公寓
参数require(:公寓).permit({公寓规则关系属性:[:房屋规则id,:州,房屋规则属性:[:公寓规则id]}
p、 我尝试了多种其他方法来解决这个问题,但有很多方法:对于我需要处理的一些繁重的SQL查询,通过关系似乎是唯一可行的方法。

试试这个
class ApartmentRuleRelation < ApplicationRecord
  belongs_to :apartment
  belongs_to :house_rule
end
form.html.erb

<%= form_for [:landlord, :profile, @apartment] do |form| %>
  <% HouseRule.all.each do |rule| %>
    <%= form.fields_for :apartment_rule_relations do |ar_form| %>
       <%= ar_form.radio_button(:state, true) %>
       <%= ar_form.radio_button(:state, false) %>
       <%= ar_form.hidden_field(house_rule_id: rule.id) %>
    <% end %>
  <% end %>
<% end %>

___________________________________________________________

apartments_controller.rb

params.require(:apartment).permit({ apartment_rules_relation_attributes: [:house_rule_id, :state, house_rules_attributes: [:apartment_id] ] }