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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:54:47+00:00 2026-06-14T07:54:47+00:00

I am using polymorphic relation as i have 3 models like this: class Food

  • 0

I am using polymorphic relation as i have 3 models like this:

class Food < ActiveRecord::Base
   has_many :images, as: :imageable, foreign_key: :imageable_uuid, dependent: :destroy
   accepts_nested_attributes_for :images, :allow_destroy => true
end
class MenuPhoto < ActiveRecord::Base
   has_one :image, as: :imageable, foreign_key: :imageable_uuid, dependent: :destroy
   accepts_nested_attributes_for :image
end
class Image < ActiveRecord::Base
   belongs_to :imageable, foreign_key: :imageable_uuid, :polymorphic => true
end

So in my “menu photo form”, I put it like this:

= simple_form_for @menu_photo do |f|
    = f.simple_fields_for :image_attributes do |d|
        = d.input :photo, as: :file 
        = f.submit

When i submit this form, it gives me like this:

{"menu_photo"=>{
    "image_attributes"=>
        {"photo"=>"user image upload"}
     }
}

It is correct. So in “food form” i do the same:

= simple_form_for @food do |f|
    = f.simple_fields_for :images_attributes do |d|
        = d.input :photo, as: :file 
        = f.submit

What i expect:

{"food"=>{
    "images_attributes"=>[
        {"photo"=>"user image upload one"}, 
        {"photo"=>"user image upload two"}
    ]}
}

What i got:

{"food"=>{
    "images_attributes"=>
        {"photo"=>"user image upload one"}
     }
}

That gives me an error. Any solution about this one?

  • 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-14T07:54:49+00:00Added an answer on June 14, 2026 at 7:54 am

    If you defined has_many association:

    has_many :images
    

    has_many association will add some methods help you build and create object:

    collection.build(attributes = {}, …)
    collection.create(attributes = {})
    

    collection here is images. You can read more here has_many

    And when you define has_one association:

    has_one :image
    

    has_one association will add some methods help you build and create object:

    build_association(attributes = {})
    create_association(attributes = {})
    

    create_association here is image. You can read more here has_one.

    So, it will have the different way for you to create a new object associated with has_many and has_one.

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

Sidebar

Related Questions

I have a Customer model that has_many polymorphic Address objects, like so: Customer Model:
I am using polymorphic association. I have 2 models articles and events which have
     I have created a basic inheritance program using a polymorphic array. From the parent-class,
i'm using nested_form for adding and deleting nested_attributes. class Text < ActiveRecord::Base attr_accessible :attachments_attributes
If I have a polymorphic base class called Base as well as classes Derived1
I am using Polymorphic Models . Simple Question: My code below works without using
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using Ruby on Rails, how can I achieve a polymorphic has_many relationship where the
When using the std::for_each, class A; vector<A*> VectorOfAPointers; std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo)); If we have
I have a Payment Model which is polymorphic on activities class Payment belongs_to :activity,

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.