gpt4 book ai didi

ruby-on-rails - Rails 有很多通过多态的复选框

转载 作者:行者123 更新时间:2023-12-04 20:23:02 25 4
gpt4 key购买 nike

这个真让我失望! :(

我正在尝试为我的用户模型制作一个嵌套模型表单,其中有一个复选框列表,可以选中或取消选中多个商店以通过模型人员配备管理商店。

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

出于手头的目的,可以忽略其他可工作的关联。这是一个类似的模型,我最终希望与 Store 一起管理,但首先要考虑的是,因为我实在太难了。
# User Form
- for store in Store.all
%p
= check_box_tag "user[store_ids][]", store.id, @user.stores.include?(store)
= store.nickname

像这样发送我的参数:
{"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"]}}

然后错误!
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)

我知道我必须将 staffable_type 构建为“Store”,但我一整天都在尝试——有什么诀窍?

我确实将 staffable_type(和 id)列设置为 :null => false 但这不能成为导致此问题的原因,因为这需要在 Controller 中理顺,然后再点击数据库。

为什么以下在我的创建操作中不起作用:
@user.staffings.each do |s|
s.staffable_type = 'Store'
end

或者:
@user.store_ids.each do |i|
s = @user.staffings.build
s.staffable_type = 'Store'
s.staffable_id = i
end

如果尝试了许多与上述类似的事情都无济于事。任何帮助将不胜感激。

谢谢你的时间!

最佳答案

好吧,我想通了这一点。我在这里回答,所以我希望有一天可以帮助某人,但问题是对 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

关于ruby-on-rails - Rails 有很多通过多态的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4200540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com