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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:17:16+00:00 2026-06-13T13:17:16+00:00

I am using Ruby 1.9.2 and Ruby on Rails 3.2.2. I have following statements:

  • 0

I am using Ruby 1.9.2 and Ruby on Rails 3.2.2. I have following statements:

class Category < ActiveRecord::Base
  include Commentable

  acts_as_list :scope => 'category_comment_id'

  has_many :comments, :class_name => 'CategoryComment'

  # ...
end

module Commentable
  extend ActiveSupport::Concern

  included do
    acts_as_list :scope => 'comment_id'

    has_many :comments, :class_name => 'Comment'

    # Other useful method statements...
  end

  # Other useful method statements...
end

In the above code I am trying to override both acts_as_something and has_many methods added to the Category class by the including Commentable module. Both methods are stated “in the scope of” Category so the above code does not work as expected: methods are not overrode.

Is it possible to override those methods? If so, how?

  • 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-13T13:17:17+00:00Added an answer on June 13, 2026 at 1:17 pm

    You should include your module in the end of your class definition. As it is now, methods from the module get injected before the class defines its method. This is so because ruby processes and evaluates code in the top-down manner. Therefore, a little bit later it encounters class’ own definitions of the methods and overwrites those that came from the module.

    So, use this knowledge according to your intention: who should override whom. If methods from the module should take dominance over those in the class, include it in the end.

    Edit

    Given this code

    require 'active_support/core_ext'
    
    class Base
      def self.has_many what, options = {}
        define_method "many_#{what}" do
          "I am having many #{what} with #{options}"
        end
      end
    end
    
    module Commentable
      extend ActiveSupport::Concern
    
      included do
        has_many :comments, class_name: 'Comment'
      end
    end
    

    Then

    class Foo < Base
      include Commentable
      has_many :comments
    end
    
    # class overrides module
    Foo.new.many_comments # => "I am having many comments with {}"
    

    And

    class Foo < Base
      has_many :comments
      include Commentable
    end
    
    # module overrides class 
    Foo.new.many_comments # => "I am having many comments with {:class_name=>\"Comment\"}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ruby on Rails 3.2.2. I have the following scenario: # hash_params.class
I'm using ruby 1.9.2 and rails 3.2.2. I have a 'domain' model (domain.rb): class
I am using Ruby on Rails v3.2.2. I have following associations with an alias_method
I am using Ruby 1.9.2 and Ruby on Rails 3.2.2. I have the following
I am using Ruby 1.8.7 and Rails 2.3.8 and I have the following root
Using Ruby 1.9.2, Rails 3.1. I have the following code: # review.rb def calculate_rating(reviewable)
I am using Ruby on Rails v3.0.9 and I have the following code in
I am using Ruby 1.9.2, Rails 3.1. I have the following: # review.rb def
I have created a blog application using Ruby on Rails which includes the ability
I am using Ruby on Rails 3.2.2 and MySQL. I have a method (an

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.