質問

この非常に私は、以下が含まれます。:(

私として生まれ変わろうとしているネストしたモデル形成のためのダイアログのユーザーモデルのチェックボックスのリストで複数の店舗でチェックボックスやチェック管理の店舗を通じてモデルの人材派遣.

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

目的にお手元にstaffable協会は無視できます。この類似モデルといった管理ととも店舗が一番初めに思っていstumpedしています。

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

送っparams沿いど:

{"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どの店舗もってすべての日--何のトリック?

いのstaffable_type(id)カラムに設定:nullの場合=>falseができないのが原因であることが必要寧にコントローラなのDBます。

のはなぜですか以下にない私の作成アクション:

@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

場合について多くのこと上記と同様に無い.他の助けが大規模ください。

りの時間!

役に立ちましたか?

解決

えきっかけになるかもしれないとこです。私の答えはここだったので思いる人がいつか、そこで問題になった基本的な監督を行いまの違いを多くを通じて、所属する多くの会

だいたい場合取扱いをチェックボックスまたはmultiselectリストには多くありません'_ids'メソッドで生成されるのを記述する必要がありますね。

参照ポールハーモニーランドのパスポートの記事はこちらhttp://paulbarry.com/articles/2007/10/24/has_many-through-checkboxes

多様なもので複雑なものだっ不良いものになったりだとは言わないも基本的なことになる釣す--たってくれました。

以下のコードから変更したポールのブログとしての取り扱いについてこの場を取り扱う多型が多くら(基本的に使用する必要があり、属性のハッシュとお書きの上、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 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top