Created
December 4, 2013 11:15
-
-
Save Fire-Dragon-DoL/7785962 to your computer and use it in GitHub Desktop.
Very weird arel constructs
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
| # FIXME: Impossible to store the joined table in memory | |
| def self.all_diseases_for_cat(cat) | |
| cat_diseases = self.arel_table | |
| diseases = Disease.arel_table | |
| # .joins("RIGHT OUTER JOIN #{ Disease.table_name } | |
| # ON #{ self.table_name }.#{ cat_diseases[:disease_id].name } | |
| # = #{ Disease.table_name }.#{ diseases[:id].name }") | |
| # RightOuterJoin created by me because I was bored | |
| scoped | |
| .joins( | |
| cat_diseases.join(diseases, Arel::Nodes::RightOuterJoin) | |
| .on(cat_diseases[:disease_id].eq(diseases[:id])) | |
| .join_sources | |
| ) | |
| .where( | |
| cat_diseases[:cat_id].eq(cat.id) | |
| .or( | |
| cat_diseases[:cat_id].eq(nil) | |
| ) | |
| ) | |
| end | |
| module Arel | |
| module Nodes | |
| module NamedFunctions | |
| class Concat < Arel::Nodes::NamedFunction | |
| def initialize(field_ary, aliaz=nil) | |
| super('CONCAT', field_ary, aliaz) | |
| end | |
| end | |
| end | |
| end | |
| end | |
| def self.concat_func | |
| cat_colors = self.arel_table | |
| @@func_concat ||= Arel::Nodes::NamedFunctions::Concat.new( | |
| [ cat_colors[:code], | |
| " , ", | |
| cat_colors[:name] ] | |
| ) | |
| @@func_concat | |
| end | |
| def self.where_full_name(query) | |
| cat_colors = self.arel_table | |
| scoped | |
| .order(:code, :name) | |
| .where( | |
| concat_func.eq(query) | |
| ) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment