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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:34:23+00:00 2026-06-10T10:34:23+00:00

I have 4 common functions in every Model: #Returns TRUE or FALSE depending on

  • 0

I have 4 common functions in every Model:

#Returns TRUE or FALSE depending on whether the column could be null or not
def self.null?(column)
  columns_hash[column].null
end

#Custom delete function to change a state (deleted is a field)
def custom_delete
  deleted = true
  save
end

def str_created_at(format = "%d/%m/%Y %I:%M %p")
  return created_at.in_time_zone.strftime(format)
end

def str_updated_at(format = "%d/%m/%Y %I:%M %p")
  return updated_at.in_time_zone.strftime(format)
end

I tried to move these 4 functions (1 of them is abstract: null?) to a single module with no luck:

#config/application.rb
config.autoload_paths += Dir["#{config.root}/lib/**/"]

#app/models/post.rb
class Post < ActiveRecord::Base
    include BaseModel
    default_scope where(:deleted => false)
end

#lib/base_model.rb
module BaseModel

    def self.included(base)
        base.extend ClassMethods
    end

    module InstanceMethods

        def custom_delete
            deleted = true
            save
        end

        def str_created_at(format = "%d/%m/%Y %I:%M %p")
            return created_at.in_time_zone.strftime(format)
        end

        def str_updated_at(format = "%d/%m/%Y %I:%M %p")
            return updated_at.in_time_zone.strftime(format)
        end
    end 

    module ClassMethods
        include BaseModel::InstanceMethods

        def self.null?(column)
            columns_hash[column].null
        end
    end

end

In Rails console:

> Post.null?("title")
> NoMethodError: undefined method 'null?' for #<Class:0x3f075c0>
> post = Post.first
> post.str_created_at
> NoMethodError: undefined method 'str_created_at' for #<Post:0x2975190>

Is there any way to get these functions working fine? I found this code here on Stackoverflow but seems to be not working, at least not Rails3

I’d like to have the possibility to add these functions with only 1 line:
include BaseModel

So I can add it to other models as well.

  • 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-10T10:34:25+00:00Added an answer on June 10, 2026 at 10:34 am

    Concerns are the solution. It’s really important to be able to modularize your application, both for rails apps that for libraries. Note that this approach can be pretty cool also when hanging with Engines.

    Concerns must be placed in the concern folder, both for models and controllers. Just be careful to not use them too much. You must use it mainly when you share functionalities between your classes and when you want to make your model skinny. In the last case wait for it to be really big, otherwise concerns will become a trash directory and you’ll lose its effect.

    Configure application.rb

    Here you can see how to organize your model and controller concerns.

    module YourApp
      class Application < Rails::Application
        config.autoload_paths += %W(
          #{config.root}/app/controllers/concerns
          #{config.root}/app/models/concerns
        )
      end
    end
    

    Creating concerns

    You can define your concern using ActiveSupport::Concern class. It simplify the ‘including’ phase (you you do not have to use base as prefix) and it automatically load the class methods that are inside of module ClassMethod.

    Here an example of what change.

    class Message < ActiveRecord::Base
      include Trashable
    end
    
    module Trashable
      extend ActiveSupport::Concern
    
      included do
        field :new
      end
    
      module ClassMethods
        # some class methods
      end
    
      # some instance methods
      end
    

    Here you can find a nice article that explain them much more in details.

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

Sidebar

Related Questions

So I have a common module which contains processing functions for numbers and types
I have a common validation function function connect_and_update_destination() { var flag = true; //
I need to have an instance of one common object in every other object,
In Python, functions created using def and lambda have a __dict__ attribute so you
Im looking to have a common parent function like so void main (param 1,
I have common functionality that I need to access from all screens of my
I have this common issue of 'new line' in php-csv which is making me
I have a common comms library that i have written to communicate with our
I have a common script which Im including in my PHPcron files and the
I have a common resource, which I want 1 and only 1 instance of

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.