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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:11:25+00:00 2026-05-23T12:11:25+00:00

So a while ago I created a small module to serve as the methods

  • 0

So a while ago I created a small module to serve as the methods I need for a votable polymorphic association, and while it was originally meant to be used only for ActiveRecord I now want to use it with mongo, and since I’m using mongoid all of the methods for the associations I need to create in this intance have the same names and everthing here take a look at my previous code:

# config/initializers/acts_as_votable.rb

module ActsAsVotable

end

module ActiveRecord
  class Base
    class << self
      cattr_accessor :votable

      def acts_as_votable
        has_many :votes, :as => :voteable
      end

      def acts_as_voter
        has_many :votes, :as => :voter
      end

      def votable?
        method_defined? :votes
      end
    end

    def votable?
      self.class.send(:method_defined?, :votes)
    end
  end
end

And here is how it was used:

class Recipe < ActiveRecord::Base
  acts_as_votable
  # more stuff...
end

So you’ll notice two issues here, firstly, I’m extending ActiveRecord::Base, how can I make this work for any model, not just ones that inherit from ActiveRecord?, Secondly do I actually need an empty module like that ActsAsVotable one? What am I doing wrong here?

If I just put all that code into the module ActsAsVotable, shouldn’t I just be able to call includes ActsAsVotable from my model?

  • 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-23T12:11:26+00:00Added an answer on May 23, 2026 at 12:11 pm

    First, put this in an initializer or in /lib but be sure the path is loaded in your application.

    module ActsAsVotable
    
      extend ActiveSupport::Concern
    
      module InstanceMethods
        def votable?
          self.class.send(:method_defined?, :votes)
        end
      end
    
      module ClassMethods
        cattr_accessor :votable
    
        def acts_as_votable
          has_many :votes, :as => :voteable
        end
    
        def acts_as_voter
          has_many :votes, :as => :voter
        end
    
        def votable?
          method_defined? :votes
        end
    
      end
    end
    

    Then in any model:

    class User < ActiveRecord::Base
    
      include ActsAsVotable
    
    end
    

    Finally:

    User.methods.include?("acts_as_votable") # => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created SSIS packages a while ago, today I need to re-point them to
I created a little app a while ago. I created admin.py and used admin.site.register(MenuEntry)
A while ago I created a small cardgame web app for fun. The player
not so long ago I´ve created a small iphone app for my Daily use.
A while ago I created a drag-over check box list which allows you to
A little while ago I created a class to deal with my LAN networking
I have an application that I created a while ago for some unknown portable
I have a couple of functions I created a while ago for reading and
I created a cache using Soft References a while ago, but in trying to
I'm upgrading a payment management system I created a while ago. It currently has

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.