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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:56:13+00:00 2026-06-07T03:56:13+00:00

I have a transaction (extends ActiveRecord::Base). I have two different types of transactions, a

  • 0

I have a “transaction” (extends ActiveRecord::Base). I have two different types of transactions, a “Purchase” or a “Donation”. There’s enough overlap between the two that there’s no need to create two separate database tables, so I just have one table for the transactions with an “item_type” column.

However, there are different methods and validations for a Purchase and Donation, so it makes sense to break them up into two different controllers and models. Instead of creating ActiveBase models (minus the tables), I’m trying to use Modules for each of these.

Here is what the Purchase module looks like.

module Purchase
  def self.included(base)
    base.validates :amount, 
      :presence => true
  end

  def testing
    "testing"
  end
end

Here is how one is created: (this code is in the create action of the Purchases controller)

@purchase = Transaction.new(params[:purchase]).extend(Purchase)

I am able to call @purchase.testing and it returns “testing”. However, that validation is not being run.

If I include the module in the traditional fashion on the Transaction model, with “include Purchase”, then they work.

Any ideas how I can make this workflow happen? I followed this a bit: http://paulsturgess.co.uk/articles/84-how-to-include-class-and-validation-methods-using-a-module-in-ruby-on-rails

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

    If you only want to work with a single table and multiple columns, I think the preferred approach is single table inheritance. You can read about it here if you search for single table inheritance. With regards to your question, though, about how to include the module, I think you’ll want to include the module in the metaclass for @purchase so it isn’t included in all subsequent Transactions. Kind of like this (can definitely be shortened):

    @purchase = Transaction.new(params[:purchase])
    class << @purchase
      include Purchase
    end
    

    Then, your validation should work.

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

Sidebar

Related Questions

For example, I have several different merchant clients who I handle transactions for. They
I have the following code: import java.util.*; public class SellTransaction extends Transaction { private
I have a problem with transactions $backendB = new BackendBanner();//BackendBanner and ImageBanner extends Zend_Db_Table_Abstract
I have to use 3 different transaction managers in my webapp. So I wrote
I have a table definition class Transaction( val ... ) extends KeyedEntity[Long] { val
I have a transaction button on my asp.net page. If the user clicks on
I have a transaction table like so id , name , code , flag
I have a transaction table like so id , name , code , flag
I have a transaction table and I'm looking to generate a dataset to drive
I have a transaction log file in CSV format that I want use to

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.