Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7368 (closed enhancement: fixed)

Opened 1 year ago

Last modified 1 year ago

[PATCH] dynamic finders with hash attributes for creation

Reported by: hasmanyjosh Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: dynamic finder verified
Cc:

Description

This patch enhances dynamic finders to allow finding by a subset of the attributes that are used to instantiate a new object. Attributes are passed in a hash, instead of a positional list.

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

That will either find an existing tag named "rails", or create a new one while setting the user that created it.

I haven't implemented this for the non-instantiating cases (find_by_x and find_all_by_x) because I haven't seen a need for that yet.

Includes tests and doc updates.

Attachments

dynamic_finders_with_hash_attributes.diff (3.1 kB) - added by hasmanyjosh on 01/25/07 05:22:18.
dynamic finders with hash attributes for creation

Change History

01/25/07 05:22:18 changed by hasmanyjosh

  • attachment dynamic_finders_with_hash_attributes.diff added.

dynamic finders with hash attributes for creation

01/25/07 05:23:00 changed by hasmanyjosh

  • summary changed from [PATCH] dynamic finder enhancement to [PATCH] dynamic finders with hash attributes for creation.

(in reply to: ↑ description ) 02/05/07 23:40:11 changed by seanabrahams

Useful.

I came across this looking for functionality like the following:

Tag.find_or_create_by_params(params[:tag])

which I believe would be quite useful and will look into creating a patch for when time permits. Until then, if anyone wants to implement this, please do.

Cheers.

Replying to hasmanyjosh:

This patch enhances dynamic finders to allow finding by a subset of the attributes that are used to instantiate a new object. Attributes are passed in a hash, instead of a positional list. {{{ Tag.find_or_create_by_name(:name => "rails", :creator => current_user) }}} That will either find an existing tag named "rails", or create a new one while setting the user that created it. I haven't implemented this for the non-instantiating cases (find_by_x and find_all_by_x) because I haven't seen a need for that yet. Includes tests and doc updates.

03/13/07 23:13:40 changed by josh

  • keywords changed from dynamic finder to dynamic finder verified.

03/14/07 01:13:02 changed by bitsweat

  • status changed from new to closed.
  • type changed from defect to enhancement.
  • resolution set to fixed.
  • milestone changed from 1.2.3 to 1.x.