Rails有很多通过多态复选框

bla*_*ula 5 forms checkbox ruby-on-rails has-many-polymorphs has-many-through

这个真的让我失望!:(

我正在尝试为我的用户模型创建一个嵌套模型表单,其中包含一个复选框列表,其中可以检查或取消选中多个商店以通过模型人员配置来管理商店.

class Staffing < ActiveRecord::Base
  # Associations
  belongs_to :user
  belongs_to :staffable, :polymorphic => true
  belongs_to :store,     :class_name => "Store",
                         :foreign_key => "staffable_id"
end

class User < ActiveRecord::Base
  # Includes
  acts_as_authentic

  # Associations
  has_many :staffings, :dependent => :destroy
  has_many :stores, :through => :staffings

  # Nested Attributes
  accepts_nested_attributes_for :staffings, :allow_destroy => true
end

class Store < ActiveRecord::Base
  # Associations
  has_many :staffings, :as => :staffable, :dependent => :destroy
  has_many :users, :through => :staffings
end


# Users Controller
def create
  @user = User.new(params[:user])
  flash.now[:notice] = "#{@user.name} was successfully created." if @user.save
  respond_with @user
end


def update
  @user = User.find(params[:id])
  params[:user][:store_ids] ||= []
  @user.update_attributes(params[:user])
  flash.now[:notice] = "#{@user.name} was successfully updated."
  respond_with @user
end
Run Code Online (Sandbox Code Playgroud)

出于手头的目的,可以忽略其他人员协会.这是一个类似的模型,我最终希望与商店一起管理,但首先是第一件事,因为我很难过.

# User Form
- for store in Store.all
  %p
    = check_box_tag "user[store_ids][]", store.id, @user.stores.include?(store)
    = store.nickname
Run Code Online (Sandbox Code Playgroud)

像这样发送我的参数:

{"utf8"=>"?",
 "authenticity_token"=>"blub-blub-blub=",
 "user"=>{"login"=>"hey.buddy",
 "email"=>"test@hey.net",
 "role"=>"admin",
 "hq"=>"1",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]",
 "store_ids"=>["3",
 "4"]}}
Run Code Online (Sandbox Code Playgroud)

然后错误!

Mysql2::Error: Column 'staffable_type' cannot be null: INSERT INTO `staffings` (`created_at`, `staffable_id`, `staffable_type`, `updated_at`, `user_id`) VALUES ('2010-11-17 00:30:24', 3, NULL, '2010-11-17 00:30:24', 102)
Run Code Online (Sandbox Code Playgroud)

我知道我必须将staffable_type构建为"Store",但我一整天都在尝试 - 这是什么诀窍?

我确实将staffable_type(和id)列设置为:null => false但是这不能成为原因,因为无论如何都需要在控制器中理清它.

为什么以下内容在我的创建操作中不起作用:

@user.staffings.each do |s|
  s.staffable_type = 'Store'
end
Run Code Online (Sandbox Code Playgroud)

要么:

@user.store_ids.each do |i|
  s = @user.staffings.build
  s.staffable_type = 'Store'
  s.staffable_id = i
end
Run Code Online (Sandbox Code Playgroud)

如果尝试过类似上面的许多事情都无济于事.任何帮助都将受到大力赞赏.

谢谢你的时间!

bla*_*ula 4

好吧,我想出了这个。我在这里回答是为了希望有一天能帮助别人,但问题是对“Has Many Through”和“Has And Belongs To Many Associations”之间差异的基本疏忽。

如果您想处理 Has Many Through 中的复选框或多选列表,则关联不会为您生成“_ids”方法,您需要自己编写一个方法。

请参阅 Paul Barry 的文章: http://paulbarry.com/articles/2007/10/24/has_many-through-checkboxes

多态的东西可能很复杂,但如果你真的陷入困境,也许退后一步,确保它不是更基本的东西让你陷入困境——对我有用!

下面是从 Paul 的博客中修改的代码,说明如果您正在处理多态性,如何处理这个问题(基本上您只需要使用属性哈希并编写您的 polymorphic_type 属性):

attr_accessor :store_ids
after_save :update_stores

#after_save callback to handle group_ids
def update_stores
  unless store_ids.nil?
    self.staffings.each do |staffing|
      staffing.destroy unless store_ids.include?(staffing.staffable_id.to_s)
      store_ids.delete(staffing.staffable_id.to_s)
    end 
    store_ids.each do |s|
      self.staffings.create(attributes = {:staffable_id => s, :staffable_type => 'Store'}) unless s.blank?
    end
    reload
    self.store_ids = nil
  end
end 
Run Code Online (Sandbox Code Playgroud)