通过与Rails 4的连接表允许has_many的额外参数

时间:2015-03-25 03:28:19

标签: ruby-on-rails ruby-on-rails-4 has-many-through

我的模型设置如下:

class Contract <ActiveRecord::Base
  has_many :contract_upgrades
  has_many :upgrades, through: :contract_upgrades
end

我的升级模式

class Upgrade < ActiveRecord::Base
  has_many :contract_upgrades
  has_many :contracts, through: :contract_upgrades
end

我的ContractUpgrade模型

# schema
  t.integer :contract_id
  t.integer :upgrade_id
  t.integer :qty
class ContractUpgrade < ActiveRecord::Base
  belongs_to :contract
  belongs_to :upgrade
end

我的合约表格看起来像这样......

- @upgrades.available.each do |upgrade|
  %tr{class: "#{upgrade.id}", id: 'upgrade-checkboxes'}
    %td
      .upgrade-checkboxes
        = check_box_tag "contract[upgrade_ids][]", upgrade.id, @contract.upgrades.include?(upgrade), { upgrade_id: upgrade.id }
        = label_tag "contract_upgrade_#{upgrade.id}", upgrade.name
    %td
      = number_to_currency("#{upgrade.price}")
  - if upgrade.allow_each

这里有点棘手......

    %td
      %input{type: 'text', placeholder: 'Qty'} # displays text_field box
      = number_field_tag # displays text_field box

当检查升级check_box时,upgrade.id和contract.id保存它应该并且工作正常。我需要能够在那些可以有多个的升级上输入数量。在我的Contracts控制器中,upgrade_ids通过{upgrade_ids:[]}列入白名单,无论我在qty字段中有什么内容,我都会得到一个unpermitted_pa​​rameter:qty ...

更新

如果有人可以让Rails Guides Associations: has_many_through example工作,我就能弄明白我该怎么做才能做我真正需要做的事情。在此示例中,直接来自rails指南,连接表具有appointment_date属性。

0 个答案:

没有答案