Skip to content

Instantly share code, notes, and snippets.

@jirihradil
Created June 17, 2011 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jirihradil/141c85d60f6c80605690 to your computer and use it in GitHub Desktop.
Save jirihradil/141c85d60f6c80605690 to your computer and use it in GitHub Desktop.
Active Record a stavění dotazů
class Site < ActiveRecord::Base
def self.build_conditions(ids, name=nil, state=nil)
cond = []
cond << send(:sanitize_sql_array, ["id NOT IN (?)", ids]) unless ids.empty?
cond << send(:sanitize_sql_array, ["name = ? ", name]) unless name
cond << send(:sanitize_sql_array, ["state = ? ", state]) unless state
cond.join(" and ")
end
end
Now somewhere in your controller:
Site.all(:conditions => Site.build_conditions([1,2]))
Site.all(:conditions => Site.build_conditions(nil, "ABC"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment