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

  • SEARCH
  • Home
  • 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 8127679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:37:01+00:00 2026-06-06T07:37:01+00:00

I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable

  • 0

I have two models,

Landscape:

class Landscape < ActiveRecord::Base
  has_many :images, :as => :imageable
  accepts_nested_attributes_for :images, :allow_destroy => true  
  attr_accessible :id, :name, :city, :state, :zip, :gps, :images, :images_attributes, :address1

  def autosave_associated_records_for_images 
    logger.info "in autosave"
    images.each do |image|
      if new_image = Image.find(image.id) then
        new_image.save!
      else
        self.images.build(image)
      end
    end
  end
end

Image:

class Image < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true
end

I’m sending post and put requests from an iPhone to update/create a landscape record with json. Here’s an example POST request to create a new landscape record and a new associated image record

{
  "landscape":{
    "id":0,
    "name":"New Landscape",
    "city":"The City",
    "state":"LA",
    "zip":"71457",
    "images_attributes":[
      {
        "id":0,
        "image_data":"image data image data image data",
        "is_thumbnail":1
      }
     ],
    "address1":"1800 Fancy Road"
  }

}

When the server receives this, it spits out

ActiveRecord::RecordNotFound (Couldn't find Image with ID=0 for Landscape with ID=0):

So this seems to be some kind of circular reference issue, but it’s not clear how to go about fixing it. Also of interest is that the autosave_associated_records_for_images doesn’t ever seem to be called (also there is almost no documentation for that function, I had to look at the rails source).

I’ve read just about every SO post on accepts_nested_attributes_for with no luck.

Update

I have the records creating now, but I can’t get rails to pass the image data back to the Image model. Let me illustrate:

Started POST "/landscapes" for 127.0.0.1 at 2011-07-22 19:43:23 -0500
  Processing by LandscapesController#create as JSON
  Parameters: {"landscape"=>{"name"=>"asdf", "id"=>0, "address1"=>"asdf", "city"=>"asdf", "images_attributes"=>[{"id"=>0, "image_data"=>"Im a bunch of image data image data image data", "is_thumbnail"=>1}]}}
  SQL (0.1ms)  BEGIN
  SQL (1.6ms)  describe `landscapes`
  AREL (0.2ms)  INSERT INTO `landscapes` (`address1`, `city`, `gps`, `name`, `state`, `zip`, `updated_at`, `created_at`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, '2011-07-23 00:43:23', '2011-07-23 00:43:23')
  SQL (1.0ms)  describe `images`
  AREL (0.1ms)  INSERT INTO `images` (`image_caption`, `image_data`, `is_thumbnail`, `created_at`, `updated_at`, `imageable_id`, `imageable_type`) VALUES (NULL, NULL, NULL, '2011-07-23 00:43:23', '2011-07-23 00:43:23', 46, 'Landscape')
  SQL (0.2ms)  COMMIT
Completed 201 Created in 37ms (Views: 2.2ms | ActiveRecord: 14.5ms)

That’s what happens when I don’t define autosave_asociated_records_for_images. However, if I define it like so:

def autosave_associated_records_for_images
  logger.info "in autosave"
  logger.info images.to_s
end

I see the following output:

Started POST "/landscapes" for 127.0.0.1 at 2011-07-22 19:50:57 -0500
  Processing by LandscapesController#create as JSON
  Parameters: {"landscape"=>{"name"=>"asdf", "id"=>0, "address1"=>"asdf", "city"=>"asdf", "images_attributes"=>[{"id"=>0, "image_data"=>"Im a bunch of image data image data image data", "is_thumbnail"=>1}]}}
  SQL (0.1ms)  BEGIN
  SQL (1.6ms)  describe `landscapes`
  AREL (0.2ms)  INSERT INTO `landscapes` (`address1`, `city`, `gps`, `name`, `state`, `zip`, `updated_at`, `created_at`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, '2011-07-23 00:50:57', '2011-07-23 00:50:57')
in autosave
[#<Image id: nil, image_caption: nil, image_data: nil, is_thumbnail: nil, created_at: nil, updated_at: nil, imageable_id: nil, imageable_type: "Landscape">]
  SQL (0.2ms)  COMMIT
Completed 201 Created in 32ms (Views: 2.2ms | ActiveRecord: 2.1ms)

This is very strange! It’s creating the relationship properly, but it’s not actually populating the database with the data I’m sending. Any ideas?

  • 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-06-06T07:37:03+00:00Added an answer on June 6, 2026 at 7:37 am

    I never got either of the solutions here to work. What I did instead was do this manually in the controller by looping through the params.

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

Sidebar

Related Questions

I have two models Horse and Race. class Horse < ActiveRecord::Base has_many :races end
I have two models: class Sentence < ActiveRecord::Base attr_accessible :sentence_id, :authority_name #... end class
I have two models. order and line_item. class Order < ActiveRecord::Base has_many :line_items has_many
I have two models: class Member < ActiveRecord::Base has_many :member_tags end and class MemberTag
I have two models as follows: class Game << ActiveRecord::Base has_many :bells end class
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have two models class Subscription < ActiveRecord::Base belongs_to :client end class Client <
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =

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.