ruby-on-rails – Rails 3 has_many:通过Form

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails 3 has_many:通过Form前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
无法弄清楚为什么这不起作用.首次使用:has_many => :通过

继续保持未初始化的持续User :: Employmentship

class User < ActiveRecord::Base
  has_many :employmentships
  has_many :companies,:through => :employmentships
  accepts_nested_attributes_for :employmentships,:allow_destroy => true,:reject_if => proc { |obj| obj.blank? }
  attr_accessible :email,:password,:password_confirmation,:firstname,:lastname,:username,:role,:company_ids
end

class Company < ActiveRecord::Base
  has_many :employmentships
  has_many :users,:through => :employmentships
end


/views/users/_form.html.erb
<p>
   <%= for company in Company.all do %>
     <%= check_Box_tag "user[company_ids][]",company.id,@user.companies.include?(company) %>
     <%= company.name%>
   <% end %>
</p>

编辑 – 如果我将@ user.companies.include?(公司)更改为false我得到表单,但没有更新.

编辑2 –

class Employmentship < ActiveRecord::Base
  belongs_to :company
  belongs_to :user
  attr_accessor :company_id,:user_id
end

解决方法

您的就业模式在哪里? has_many_through用于浏览另一个模型.

猜你在找的Ruby相关文章