# Modifying ActiveRecord::Base - where?

**URL:** <https://www.ruby-forum.com/t/modifying-activerecord-base-where/52477>\
**Category:** Rails\
**Created:** [December 6, 2005, 6:19am UTC](https://www.ruby-forum.com/t/modifying-activerecord-base-where/52477 "2005-12-06T06:19:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 6, 2005, 6:19am UTC](https://www.ruby-forum.com/t/modifying-activerecord-base-where/52477/1 "2005-12-06T06:19:17Z")

</div>

i need to add a method to ActiveRecord::Base. to be specific

class ActiveRecord::Base  
def self::restful \*a  
@restful = a.shift || @restful  
end  
end

so i can

class MyModel \< ActiveRecord::Base  
has\_one :foobar  
restful true  
end

where is the currently preferred location to place a file which does  
this?

## -a

# =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | – bodhicaryavatara

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 6, 2005, 7:24am UTC](https://www.ruby-forum.com/t/modifying-activerecord-base-where/52477/2 "2005-12-06T07:24:24Z")

</div>

On Dec 5, 2005, at 10:16 PM, Ara.T.Howard wrote:

> class MyModel \< ActiveRecord::Base  
> has\_one :foobar  
> restful true  
> end
> 
> where is the currently preferred location to place a file which  
> does this?
> 
> -a

Traditionally, it would go in your lib/ folder and then you’d add  
something like ‘require “my\_active\_record.rb”’ in your environment.rb  
file. I usually go one step further and add a ‘patches’ folder  
within ‘lib’ so I can differentiate between rails additions and other  
libraries.

Nowadays, however, it seems that plugins may do exactly what you’re  
after. But it seems a little overkill to put one tiny method like  
that into a plugin.

Duane J.  
(canadaduane)

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 6, 2005, 8:09am UTC](https://www.ruby-forum.com/t/modifying-activerecord-base-where/52477/3 "2005-12-06T08:09:14Z")

</div>

On Mon, 5 Dec 2005, Duane J. wrote:

> Traditionally, it would go in your lib/ folder and then you’d add something  
> like ‘require “my\_active\_record.rb”’ in your environment.rb file. I usually  
> go one step further and add a ‘patches’ folder within ‘lib’ so I can  
> differentiate between rails additions and other libraries.

hmmm. i’d like to have it autoloaded - guess i could put the require in  
my  
environment.rb then. that kosher?

> Nowadays, however, it seems that plugins may do exactly what you’re after.  
> But it seems a little overkill to put one tiny method like that into a  
> plugin.

agreed.

## -a

# =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | – bodhicaryavatara

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 6, 2005, 7:07pm UTC](https://www.ruby-forum.com/t/modifying-activerecord-base-where/52477/4 "2005-12-06T19:07:07Z")

</div>

wiki about modifying rails core

[http://wiki.rubyonrails.com/rails/pages/HowToWritePluginToModifyRailsCore](http://wiki.rubyonrails.com/rails/pages/HowToWritePluginToModifyRailsCore)

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 6, 2005, 7:27pm UTC](https://www.ruby-forum.com/t/modifying-activerecord-base-where/52477/5 "2005-12-06T19:27:31Z")

</div>

On Tue, 6 Dec 2005, Peter M. wrote:

> wiki about modifying rails core
> 
> [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowToWritePluginToModifyRailsCore)

perfect. thanks.

## -a

# =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | – bodhicaryavatara

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 6, 2005, 5:49pm UTC](https://www.ruby-forum.com/t/modifying-activerecord-base-where/52477/6 "2005-12-06T17:49:27Z")

</div>

On Dec 5, 2005, at 10:26 PM, Ara.T.Howard wrote:

> require in my
> 
> ## -a

Ara- FYI you can require it with require\_dependency if you want it to  
be reloaded in dev mode in case you want to make changes to it  
without restarting the webserver.

Cheers-  
-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 7, 2005, 2:35am UTC](https://www.ruby-forum.com/t/modifying-activerecord-base-where/52477/7 "2005-12-07T02:35:23Z")

</div>

On Dec 5, 2005, at 11:26 PM, Ara.T.Howard wrote:

> require in my  
> environment.rb then. that kosher?

Yes. In fact, here’s what I do in environment.rb:

# Auto-require method to include all libraries within a directory

def require\_all\_within(directory, description = “library”)  
Find.find(directory) do |path|  
if File.file?(path) && File.extname(path) == “.rb”  
expanded\_file = File.expand\_path(path)  
begin  
STDERR \<\< “Require #{description} #{File.basename(path)}\n”  
if require\_dependency(expanded\_file)  
rescue Exception =\> e  
STDERR \<\< “\nError requiring #{expanded\_file}\n”  
STDERR \<\< e.to\_s + “\n”  
end  
end  
end  
end

require\_all\_within("#{RAILS\_ROOT}/lib/enhancements")  
require\_all\_within("#{RAILS\_ROOT}/lib/patches")

That way all my enhancements and patches are pre-loaded on bootup,  
plus I get the benefit of Rails auto-loading if a filename within  
lib/ matches a class or module (the usual behavior).

Duane J.  
(canadaduane)
