Sha256: 4cc699fd93c25a4bce0acf60bfe37660e6490f9e328f9f1efa1d0f22d8e7b1b7
Contents?: true
Size: 1.9 KB
Versions: 1
Compression:
Stored size: 1.9 KB
Contents
= Inequal Opportunity inequal_opportunity exists because this does not work in ActiveRecord: Doctor.all(:joins => :patients, :conditions => {:patients => ['age > ?', 20]}) You will get an unknown column exception looking for `doctors`.`age`. Instead, you need to write: Doctor.all(:joins => :pateints, :conditions => ['`patients`.`age` > ?', 20]) Putting the string table name in the query annoyed me. On top of that, I always wanted a way to eliminate strings from my named scopes and queries. So now with inequal_opportunity you can write: Doctor.all(:joins => :patients, :conditions => {:patients => {:age => gt(20)}}) Not only is it prettier (hashed), but ActiveRecord will keep track of table names for you. ActiveRecord looks for Array and Range types to decide whether to use 'IN' or 'BETWEEN' instead of the normal '=' as the comparison operator when in generating SQL. inequal_opportunity extends that pattern by wrapping the value in a series of ActiveRecord::Inequality::Base classes. Just wrap the value in one of the following helper functions: gte() => >= gt() => > lte() => <= le() => < ne() => <> ne(nil) => IS NOT and the appropriate SQL will be generated. This works in finds, as shown above, in counts: People.count(:age => gt(20)) in named scopes: class People < AR::B named_scope :underage, :conditions => {:age => lte(18)} end in default scopes: class Feedback < AR::B default_scope :conditions => {:type => ne('spam')} end and pretty much everywhere else I've tested. Test coverage is kind of sparse right now, and it's only been tested on MySQL. I also am not completely satisfied with the way I alias ActiveRecord::Base.expand_range_bind_variables, but it works. == License inequal_opportunity is released under the MIT license. == Support Just email me at ryan@angilly.com with questions, bugs, or patches.
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ryana-inequal_opportunity-0.1.3 | README |