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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:32:41+00:00 2026-06-03T06:32:41+00:00

Consider the following polymorphic relationship class Person has_many :photos, :as => :attachable end class

  • 0

Consider the following polymorphic relationship

class Person
  has_many :photos, :as => :attachable
end

class Animal
  has_many :photos, :as => :attachable
end

class Photo
  belongs_to :attachable, :polymorphic => true
end

The view pages of a Person or Animal object contains a gallery of their respective photos.
The pages contain the following code:

...
  <%= link_to(image_tag(photo.image_url(:thumb).to_s), [@attachable, photo]) %>
...

However in the view page of my photo, I want to present different information depending on whether it belongs to a Person or an Animal. For example, if it is a Person it should display his/her height, list of pets; if it is an Animal, it should display its breed, owner, etc.

What is the best solution to do this?

I can think of two possible solutions:

One way could be to direct to a separate controller action in the photo for each class.
Something like

<%= link_to(image_tag(photo.image_url(:thumb).to_s), show_person_path[@attachable, photo]) %>
<%= link_to(image_tag(photo.image_url(:thumb).to_s), show_animal_path[@attachable, photo]) %>

However I think this approach introduces improper delegation of view logic in the polymorphic class.

Another approach is to introduce a controller action in each Person and Animal class to display their corresponding information. The gallery should link to this action instead of the one above, and the action should render the photo.

However I feel like the corresponding information to be displayed is only few to merit its own action for each of the class. Maybe helpers are enough, but I am not sure of the right approach (please correct me if the above assumptions are true).

  • 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-03T06:32:43+00:00Added an answer on June 3, 2026 at 6:32 am

    If the differences between Animal and Person do not justify a separate controller, you could have one controller and in the action for the gallery, just render different views based on the type of your @attachable:

    if @attachable.is_a? Person
      render 'gallery_person'
    elsif @attachable.is_a? Animal
      render 'gallery_animal'
    end
    

    The only downside I see here is that you would need to come back and extend the logic whenever you introduce a new type of attachable. You could add a default gallery view to mitigate this.

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

Sidebar

Related Questions

Consider following 2 programs giving same error First calss: public class Testing { Testing
Consider the following snippet: public class ReflectionTest { public static void main(String[] args) {
Let's consider the following simplified Resource hierarchy: public abstract class Resource { static public
Consider following example: class CBase abstract { protected: CBase() { } }; I could
Consider following XAML: <Window x:Class=WpfApplication1.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350 Width=525> <StackPanel> <TextBlock Text={Binding Dic[foo]}
Consider following: Partial View: <%= Html.ListBox(BlackList, Model.Select( x => new SelectListItem { Text =
Consider the (highly simplified) following case: class Dispatcher { public: receive() {/*implementation*/}; // callback
I'm having a devil of a time understanding references. Consider the following code: class
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Consider following class class test { public: test(int x){ cout<< test \n; } };

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.