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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:09:54+00:00 2026-06-17T22:09:54+00:00

As referenced in my earlier post rails has_many manager , I am trying to

  • 0

As referenced in my earlier post rails has_many manager, I am trying to create a polymorphic imaging system which will allow any item to inherit the ability to have a cover photo and additional photos.

To accomplish that kind of imaging system, I sided with a polymorphic model with belongs_to :imageable and extended its active record capabilities out to a module named Imageable.

My main question is, given that for example we have a class called Object, how can I create a form that only targets the first Object’s has_many association (the cover), and then separately administer the other has_many associations?

The form would look like..

— Form for cover photo —-

Upload button for object[image_attributes][0][public_id]

— Form for additional photos —

Upload button for object[image_attributes[1][public_id]

image.rb

class Image < ActiveRecord::Base

  attr_accessible :public_id

  # Setup the interface that models will use
  belongs_to :imageable, :polymorphic => true
end

Imageable.rb

module Imageable
  extend ActiveSupport::Concern

  included do
    has_many :images, :as => :imageable, :dependent => :destroy # remove this from your     model file
    accepts_nested_attributes_for :images

    validates :images, :presence => { :message => "At least one image is required" }
  end

  def cover
    cover = images.where(:cover => true).first
    if not cover
        return Image.new
    end
    return cover
  end

  def additional_images
    images.where(:cover => false).all
  end

end

Form

<%= form.semantic_fields_for :images do |image_fields| %>

    <%= image_fields.cl_image_upload(:public_id, :crop => :limit, :width => 1000, :height => 1000, 
                      :html => {:class => "cloudinary-fileupload"}) %> 
    ...

The above produces appropriate routes like object[image_attributes][0][public_id]

Thanks!

  • 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-17T22:09:56+00:00Added an answer on June 17, 2026 at 10:09 pm

    I would recommend that you model your relationships slightly differently by using an explicit has_one relationship from the ‘Object’ to the cover Imageable and a separate has_many relationship for the additional images.

    If you want all the images in the same collection then have a look at this post:
    How to apply a scope to an association when using fields_for?
    It explains how you can specify a ‘scope’ or subset of the entries in the has_many collection when setting up the fields_for helper. It should work with the semantic_fields_for helper as well since it simply wraps the Rails fields_for helper.

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

Sidebar

Related Questions

I've referenced System.Xml: using System.Xml; Then in this line: XmlDocument xdoc = new XmlDocument();
I have a variable which is referenced a lot. It started out as an
I had some good insight to an earlier post regarding this, but I have
I posted a question earlier which was answered properly and led me to find
In reference to an earlier post ( PDF Report generation ) I have decided
This appears similar to an earlier post: ld cannot find an existing library But
I want my unit test project to use System.Web.MVC version 4.0.0.0 always, if any
In reference to my earlier post : My HTML <table class=table table-bordered table-condensed table-striped>
I'm trying to resurrect an old (1999 or earlier) project written in Scheme (PLT-Scheme,
How can I reference by code a button's tag value which I assigned earlier?

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.