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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:28:29+00:00 2026-06-14T03:28:29+00:00

I have a Rails app with several models with the same structure: class Item1

  • 0

I have a Rails app with several models with the same structure:

class Item1 < ActiveRecord::Base
  WIDTH = 100
  HEIGHT = 100
  has_attached_file :image, styles: {original: "#{WIDTH}x#{HEIGHT}"}
  validates_attachment :image, :presence => true
end


class Item2 < ActiveRecord::Base
  WIDTH = 200
  HEIGHT = 200
  has_attached_file :image, styles: {original: "#{WIDTH}x#{HEIGHT}"}
  validates_attachment :image, :presence => true
end

The actual code is more complicated but that’s enough for simplicity.

I think I can put the common part of the code in one place and then use it in all models.

Here is what comes to my mind:

class Item1 < ActiveRecord::Base
  WIDTH = 100
  HEIGHT = 100
  extend CommonItem
end

module CommonItem
  has_attached_file :image, styles: {original: "#{WIDTH}x#{HEIGHT}"}
  validates_attachment :image, :presence => true
end

Obviously it doesn’t work for two reasons:

  1. CommonItem has no idea about class methods I invoke.
  2. WIDTH and HEIGHT constants are looked up in CommonItem instead of Item1.

I tried to use include instead of extend, some ways of class_eval and class inheritance, but none work.

It seems I am missing something obvious. Please tell me what.

  • 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-14T03:28:31+00:00Added an answer on June 14, 2026 at 3:28 am

    Here’s how I would do it:

    class Model
      def self.model_method
        puts "model_method"
      end
    end
    
    module Item
      def self.included(base)
        base.class_eval do
          p base::WIDTH, base::HEIGHT
          model_method
        end
      end
    end
    
    class Item1 < Model
      WIDTH = 100
      HEIGHT = 100
      include Item
    end
    
    class Item2 < Model
      WIDTH = 200
      HEIGHT = 200
      include Item
    end
    

    The included method is called on a module when it’s included.

    I think I’ve managed to create a similar structure that your problem has. The module is calling the method inherited by the items classes from Model class.

    Output:

    100
    100
    model_method
    200
    200
    model_method
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app with several models. I have a function that I
I have several active record models in a rails app and I would like
The main idea is that I have several worker instances of a Rails app,
I have a rails 3.2.2 app with patients who can have several types of
I have a Rails app representing a card game. There are several different types
I have a rails app with several views and layouts. One of my layouts
In my Rails App I've several models dealing with assets (attachments, pictures, logos etc.).
I currently have a Rails 3.1 app that hosts multiple sites for several different
I have a Rails App that lets Users register. Users can have several music
Something in Rails (ActiveRecord::Base.logger ?) puts all executed SQL into log/development.log. I have 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.