Rails 3 has_many :通过表单 [英] Rails 3 has_many :through Form
本文介绍了Rails 3 has_many :通过表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无法弄清楚为什么这不起作用.第一次使用 :has_many => :through
Can't figure out why this is not working. First time using :has_many => :through
不断获取未初始化的常量 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?(company) 更改为 false,我会收到表单,但没有任何更新.
EDIT - If I change @user.companies.include?(company) to false i get the form, but nothing updates.
编辑 2 -
class Employmentship < ActiveRecord::Base
belongs_to :company
belongs_to :user
attr_accessor :company_id, :user_id
end
推荐答案
你的就业模式在哪里?has_many_through 用于通过另一个模型.
Where is you employmentship model? has_many_through is for going through another model.
这篇关于Rails 3 has_many :通过表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文