【问题标题】:Rails 3 has_many :through FormRails 3 has_many:通过表单
【发布时间】:2011-02-20 08:16:46
【问题描述】:

无法弄清楚为什么这不起作用。第一次使用 :has_many => :through

不断收到uninitialized constant 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,我会收到表单,但没有任何更新。

编辑 2 -

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    你的就业模式在哪里? has_many_through 用于遍历另一个模型。

    【讨论】:

    • 哇,你绝对指向了正确的方向。以我的模特名义拥有就业而不是就业。但仍然没有建立关系。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多