Skip to content

Instantly share code, notes, and snippets.

@suvene
Last active December 12, 2015 09:19
Show Gist options
  • Save suvene/4750480 to your computer and use it in GitHub Desktop.
Save suvene/4750480 to your computer and use it in GitHub Desktop.
ActiveRecordHelper
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