Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3397718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:33:44+00:00 2026-05-18T04:33:44+00:00

This one’s really getting me down! :( I’m trying to make a nested model

  • 0

This one’s really getting me down! 🙁

I’m trying to make a nested model form for my User model with a checkbox list in it where multiple Stores can be checked or unchecked to administer the Stores through model Staffing.

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

For the purposes at hand the other staffable association can be ignored. It’s a similar model that I’d eventually want to administer alongside Store but first thing’s first since I’m so stumped as it is.

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

Sends my params along as such:

{"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"]}}

And then ERROR!

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)

I know I have to build out staffable_type as ‘Store’ but I’ve been trying all day–what’s the trick?

I do have the staffable_type (and id) columns set to :null => false but that can’t be the cause of this as this needs to be straightened out in the controller before hitting the DB anyway.

Why does the below not work in my create action:

@user.staffings.each do |s|
  s.staffable_type = 'Store'
end

or:

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

If been trying many things similar to the above to no avail. Any help would be massively appreciated.

Thanks for your time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:33:45+00:00Added an answer on May 18, 2026 at 4:33 am

    Okay I figured this one out. I’m answering here so I can hopefully help someone someday, but the problem was a basic oversight on the difference between Has Many Through and Has And Belongs To Many Associations.

    You if you want to handle checkboxes or multiselect lists in a Has Many Through, there is no ‘_ids’ method generated for you by the association and you need to write one for yourself.

    See Paul Barry’s article here:
    http://paulbarry.com/articles/2007/10/24/has_many-through-checkboxes

    Polymorphic stuff can be complicated, but if you’re stuck real good maybe take a step back and make sure it’s not something even more basic that’s messing you up– worked for me!

    Below is the code modified from Paul’s blog on how to handle this if you’re dealing with a polymorphic has many through (basically you just need to use the attributes hash and write your polymorphic_type attribute):

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has me kind of stumped. I want to make the first word
This one will take some explaining. What I've done is create a specific custom
This one has been bugging me for a while now. Is there a way
This one has me scratching my head. I'm running Subversion 1.3.1 (r19032) on Ubuntu.
This one is a case of not doing your homework.:-) Apart from dynamic loading
This one is a bit tedious in as far as explaining, so here goes.
This one has me beat; I have a WPF window with two (important for
This one has us all baffled at work. We have two services running on
This one is weird, I have a page that consists of a html table
This one's a tough one - I have a JFrame that generates JTextFields. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.