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 6852105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:19:52+00:00 2026-05-27T01:19:52+00:00

I’m having troubles understanding why save and create should be any different using these

  • 0

I’m having troubles understanding why “save” and “create” should be any different using these models with accepts_nested_attributes_for. This is my models:

class Book < ActiveRecord::Base
  has_many :pages
  has_many :picture_pages, :through => :pages, :source => :pagetype, :source_type => 'PicturePage'
  accepts_nested_attributes_for :picture_pages
end

class PicturePage < ActiveRecord::Base
  has_one :page, :as =>:pagetype
  has_one :book, :through => :pages
  accepts_nested_attributes_for :page
end

class Page < ActiveRecord::Base
  belongs_to :book
  belongs_to :pagetype, :polymorphic => true, :dependent => :destroy
end

First of all, using the save method….

b = Book.first
params = { "page_attributes"=> { "number"=>"1" }}
p = b.picture_pages.new(params)
p.save

… things work like you’d expect. Rails will automatically create a new PicturePage, with a corresponding Page join model with an assigned “number” attribute. Perfect. But if I do this:

b = Book.first
params = { "page_attributes"=> { "number"=>"1" }}
p = b.picture_pages.create(params)

… Rails will create TWO join models, one that is completely empty and one that has the number attribute. Why is this?

This is a major problem if I want to use the accepts_nested_attributes_for on the book model, because then the Book model will call “create” automatically on the PicturePage models it’s creating.

Any tips? Is this a bug in Rails?

  • 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-27T01:19:52+00:00Added an answer on May 27, 2026 at 1:19 am

    You have a lot of things going on here:

    • has_many :through ‘s
    • two accepts nested attributes going both ways
    • polymorphic association

    My tip (as you asked) is to tackle these things one by one in an agile fashion.
    Get each piece working for create and edit and view before doing the next.
    This will help you greatly going forward.

    • First get the has_many through’s working.
    • Then get the nested attributes working – and start with just one nested_attributes.
    • Finally work on the polymorphic association – but note that many folks avoid or abandon polymorphic associations as they are fiddly for some despite their apparent simplicity. I do not recommend them for rails newbies especially if combined like this.

    A final note is that this stuff will not make a lot of sense unless you read up / study rails a bit first and there a lot of web resources for that out there.

    I would try to start something like this:

    class Book < ActiveRecord::Base
      has_many :page_pictures
      has_many :pictures, :through => :page_pictures
    end
    
    class PagePicture < ActiveRecord::Base
      belongs_to :book
      belongs_to :picture
    end
    
    class Picture < ActiveRecord::Base
      has_many :page_pictures
      has_many :books, :through => :page_pictures
    end
    

    Now that allows for Pictures to be in multiple pages and books which you probably don’t need, but this is a more ‘standard’ has_many through and will work so I would start with this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.