Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several approaches :</p> <h1>Using ActiveSupport::Concern (Preferred)</h1> <p>Read the <a href="http://api.rubyonrails.org/classes/ActiveSupport/Concern.html" rel="noreferrer">ActiveSupport::Concern</a> documentation for more details.</p> <p>Create a file called <code>active_record_extension.rb</code> in the <code>lib</code> directory.</p> <pre><code>module ActiveRecordExtension extend ActiveSupport::Concern # add your instance methods here def foo "foo" end # add your static(class) methods here class_methods do #E.g: Order.top_ten def top_ten limit(10) end end end # include the extension ActiveRecord::Base.send(:include, ActiveRecordExtension) </code></pre> <p>Create a file in the <code>config/initializers</code> directory called <code>extensions.rb</code> and add the following line to the file:</p> <pre><code>require "active_record_extension" </code></pre> <h1>Inheritance (Preferred)</h1> <p>Refer to Toby's <a href="https://stackoverflow.com/questions/2328984/rails-extending-activerecordbase/2330533#2330533">answer</a>.</p> <h1>Monkey patching (Should be avoided)</h1> <p>Create a file in the <code>config/initializers</code> directory called <code>active_record_monkey_patch.rb</code>.</p> <pre><code>class ActiveRecord::Base #instance method, E.g: Order.new.foo def foo "foo" end #class method, E.g: Order.top_ten def self.top_ten limit(10) end end </code></pre> <p>The famous quote about Regular expressions by <a href="http://regex.info/blog/2006-09-15/247" rel="noreferrer">Jamie Zawinski</a> can be re-purposed to illustrate the problems associated with monkey-patching. </p> <blockquote> <p>Some people, when confronted with a problem, think “I know, I'll use monkey patching.” Now they have two problems.</p> </blockquote> <p>Monkey patching is easy and quick. But, the time and effort saved is always extracted back sometime in the future; with compound interest. These days I limit monkey patching to quickly prototype a solution in the rails console.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload