Created
June 17, 2011 09:49
-
-
Save jirihradil/141c85d60f6c80605690 to your computer and use it in GitHub Desktop.
Active Record a stavění dotazů
This file contains 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
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