Add a method to ActiveRecord?

I need a method, I call it “get_or_create(item_name)”. When an
Model.get_or_create(item_name) is called, Model.find_by_name(item_name)
will be performed fist, if there is an item with the name, the item is
return, or a new item with the name will be created and returned.

Because most of the ActiveRecord models will need this method, I try to
extend it into ActiveRecord, and put the codes into
/lib/get_or_create.rb, and require it from environment.rb.

require ‘get_or_create’
class ActiveRecord::Base
include GetOrCreate
end

Only one problem left… Can anyone tell me how to write the
get_or_create.rb ?

On 6 Dec 2007, at 10:49, Nanyang Z. wrote:

/lib/get_or_create.rb, and require it from environment.rb.

require ‘get_or_create’
class ActiveRecord::Base
include GetOrCreate
end

Only one problem left… Can anyone tell me how to write the
get_or_create.rb ?

You mean like the find_or_create_by_ dynamic finders that ActiveRecord
already has?

Fred

Frederick C. wrote:

On 6 Dec 2007, at 10:49, Nanyang Z. wrote:

/lib/get_or_create.rb, and require it from environment.rb.

require ‘get_or_create’
class ActiveRecord::Base
include GetOrCreate
end

Only one problem left… Can anyone tell me how to write the
get_or_create.rb ?

You mean like the find_or_create_by_ dynamic finders that ActiveRecord
already has?

Fred

I was just going to say that, Fred.

Alvaro P. wrote:

Frederick C. wrote:

You mean like the find_or_create_by_ dynamic finders that ActiveRecord
already has?

Fred

I was just going to say that, Fred.

Thanks! I didn’t know there is such a method already.

Anyway, can you tell me how to write these methods on my own? You can
take the exact same one for example.

On Dec 6, 2007, at 11:30 AM, Ian Lesperance wrote:

If you were going to write such a method yourself, you’d want to add
it to your model’s class, not ActiveRecord. So inside app/models/
model.rb:

I believe the OP wanted to add this to ActiveRecord because he wanted
the same functionality for all of his models.

Peace,
Phillip

If you were going to write such a method yourself, you’d want to add
it to your model’s class, not ActiveRecord. So inside app/models/
model.rb:

class Model < ActiveRecord::Base

def self.find_or_create_by_name(name)
find_by_name(name) || create(:name => name)
end

end

Remember that when a method definition begins with “self.”, it means
you’re creating a class method, not an instance method. So you’re
operating within the scope of the class, and thus don’t have to prefix
the find_by_name() and create() calls with “Model.”, since it’s
implied.

P.S. Another style of defining class methods you’ll see in some
people’s code:

class Model < ActiveRecord::Base

class << self
def find_or_create_by_name(name)
find_by_name(name) || create(:name => name)
end
end

end

As far as I can deduce, these two different styles are functionally
identical and merely a matter of preference.

On Dec 6, 3:31 am, Nanyang Z. [email protected]

Ian Lesperance wrote:

Touche. In that case, it’s probably worth noting that it’d be better
if it were written as a plug-in

Yes, Phillip was right, I wanted such a method could be accessed in all
ActiveRecord models.
Can you show me how to write such a plugin? I find no guide to extend
the ActiveRecord with this kind of method.

Touche. In that case, it’s probably worth noting that it’d be better
if it were written as a plug-in, rather than opening up
ActiveRecord::Base directly from environment.rb.

On Dec 8, 2007, at 4:58 AM, Nanyang Z. wrote:

Ian Lesperance wrote:

Touche. In that case, it’s probably worth noting that it’d be better
if it were written as a plug-in

Yes, Phillip was right, I wanted such a method could be accessed in
all
ActiveRecord models.
Can you show me how to write such a plugin? I find no guide to extend
the ActiveRecord with this kind of method.

Start here:

http://nimrod.interinter.net/plugins/trunk/acts_as_monkey/
http://wiki.rubyonrails.org/rails/pages/HowToWriteAnActsAsFoxPlugin

http://nubyonrails.com/articles/2006/05/04/the-complete-guide-to-
rails-plugins-part-i
http://nubyonrails.com/articles/2006/05/09/the-complete-guide-to-
rails-plugins-part-ii

Other links:

http://wiki.rubyonrails.org/rails/pages/HowTosPlugins


def gw
acts_as_n00b
writes_at(www.railsdev.ws)
end

I thought the OP only wanted a find_or_create_by_* method or I could be
mistaken.

He didn’t want to write a plugin.

OP, there is already a find_or_create_by method built-in to
ActiveRecord.

Model.find_or_create_by_name(:name => “Name”)

More information here:

http://www.noobkit.com/show/ruby/rails/rails-edge/activerecord-edge/activerecord/base.html#toc-dynamic-attribute-based-finders

On Dec 9, 2007 7:41 AM, Greg W. [email protected] wrote:

Can you show me how to write such a plugin? I find no guide to extend
rails-plugins-part-ii
end


Ryan B.