Last active
December 12, 2015 09:19
-
-
Save suvene/4750480 to your computer and use it in GitHub Desktop.
ActiveRecordHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecordHelper | |
module ActiveRecordHelper | |
def flatten_conditions(conditions, op = "and") | |
return nil if conditions.empty? | |
ps = [] | |
condition = conditions.collect do |c| | |
next if c.size < 1 | |
ps += c[1..(c.size)] | |
"( #{c[0]} )" | |
end.delete_if { |c| c.blank? }.join(" #{op} ") | |
[condition, ps].flatten unless condition.empty? | |
end | |
end | |
class ActiveRecord::Base | |
include ActiveRecordHelper | |
end | |
@condition = [] | |
@condition << [" prefecture_id = ? ", params[:prefecture] ] if params[:prefecture] | |
@condition << [" sex = ? ", params[:sex] ] if params[:sex] | |
@users = User.find(:all, :conditions => @conidition) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment