“Dynamic finders with hash attributes for creation”

February 19, 2009 at 4:32 pm 1 comment

Here’s a typical situation:


tag = Tag.find_by_name(name)
if tag.nil?
tag = Tag.create(:name => name, :creator => current_user)
end

You’d think the way to do that would be with the find_or_create_by_name dynamic finder, but that doesn’t work since there’s no way to search by name only, but create by name and creator. Well, that’s just not right. So here’s what we’ll do:


Tag.find_or_create_by_name(:name => name, :creator => current_user)

It’s not a big deal or something I use every day, but I like it anyway.

Source : google .

Entry filed under: ruby on rails. Tags: .

“Highlighting Text in Rails “

1 Comment Add your own

  • 1. Mike  |  March 1, 2009 at 8:27 pm

    Just passing by.Btw, your website have great content!

    _________________________________
    Making Money $150 An Hour

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Pages


Follow

Get every new post delivered to your Inbox.