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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:06:20+00:00 2026-05-18T11:06:20+00:00

This is a Rails 3 application. I have images that can be tied to

  • 0

This is a Rails 3 application.

I have images that can be tied to either a Product or a Brand. A product has an identifier and a Brand has a name.

The polymorphic relationship from Image is called “linkable”.

If I want to list the items that a particular image is linked to, I want to avoid doing a conditional in the view like this:

<% for image in Image.all %>
  <% if image.linkable.class.name=="Product" %>
    <%= image.linkable.identifier %>
  <% elsif image.linkable.class.name=="Brand" %>
    <%= image.linkable.name %>
  <% end %>
<% end %>

Of course I could just put a method inside Brand called “identifier” and use it to call “name”. But that’s not extensible if i want to add more objects that an image can be linked to. 8 years ago in Java programming I could mandate that a class implemented an Interface, but I don’t know if I can do anything like that in Ruby. I appreciate any guidance anybody can offer.

  • 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-18T11:06:21+00:00Added an answer on May 18, 2026 at 11:06 am

    You could create a module called Linkable and create the behavior methods in that. Then you extend the module in the classes where you want to add those behaviors. This way you don’t have to worry about inheriting from anything you can just mix-in the behavior.

    This is the standard Ruby way of adding common functionality to multiple classes without inheriting. You would also, by convention, name your module using a verb based adjective instead of a verb; Linkable vs. Link.

    For instance:

    module Linkable
      def link
        puts "Look, I'm linked!"
      end
    end
    
    class Product < ActiveRecord
      extend Linkable
    end
    
    class Brand < ActiveRecord
      extend Linkable
    end
    

    Of course your classes and the module will have actual functionality.

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

Sidebar

Related Questions

I have an rails a application whereby it has a page that contains a
So, i have this application that creates a zip file with images and stuff
I have had this problem w/ two seperate WYSWYG editors in my rails application
Rails noob here. I have a rails application with (in this example) three tables.
I have a rails helper in my application_helper.rb file that looks like this: def
Started working on a new application this week that is running the latest rails
I have this method on rails so that I have an image calling a
I have a twitter like web application written in Ruby on Rails that allows
My overall use case: I have a Listing model that has many images .
In my rails application I have a set of attachments that display, with a

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.