My favorites | Sign in
Project Home Wiki Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 7: Enhancement request: dynamic values and parameters
1 person starred this issue and may be notified of changes. Back to list
Status:  New
Owner:  ----


Sign in to add a comment
 
Reported by mengk...@gmail.com, Apr 9, 2007
http://www.neotrivium.com/blog/2007/4/4/out_of_the_scope_of_scope_out?language=en-US

The scoped_proxy code from the above website allows one to define something
like this:

 class User < ActiveRecord::Base
   scoped_proxy :has_login do |login|
     { :find => { :conditions => ['login = ?', login] } }
 end

This allows you to do this:

 User.has_login('tester1').find(:all)

Possible to implement this method of passing in dynamic parameter values
for scope_out?
May 2, 2007
Project Member #1 john.m.a...@gmail.com
I'm going to keep this issue open while I consider it. Right now I don't really see
the benefit of doing User.has_login('joe').find(:all) over User.find(:all,
:conditions => {:name => 'joe'}).
Labels: -Type-Defect Type-Enhancement
Jul 14, 2007
#2 daniel.m...@gmail.com
User.find_by_login('joe')
With more than one attribute:
User.find_by_login_and_password('joe', 'password')

A better example (that's contrived, certainly not practical) would be if for some
reason you stored year, month, and day for a date as separate attributes.  Then you
could do:
scoped_proxy :birthday do |bday|
  { :find => { :conditions => {:bday_year => bday.year, :bday_month => bday.month,
:bday_day => bday.day } }
end

But in that case, you could also do:
class Person
  def self.find_by_birthday(birthday, *args)
    with_scope(:find => {:conditions => {}}) do
      find(:first, *args)
    end
  end
end

and it would still work with .find_(scope)_by_birthday

I kinda like the DSL aspect of it:
User.named('Dan').born_on('May 17').find
but I question the practicality versus:
User.find_by_name_and_birthday('Dan', 'May 17')
Sign in to add a comment

Powered by Google Project Hosting