Files

Class Index [+]

Quicksearch

ActiveRecord::Associations::ThroughAssociationScope

Protected Instance Methods

build_conditions() click to toggle source
     # File lib/active_record/associations/through_association_scope.rb, line 116
116:       def build_conditions
117:         association_conditions = @reflection.options[:conditions]
118:         through_conditions = build_through_conditions
119:         source_conditions = @reflection.source_reflection.options[:conditions]
120:         uses_sti = !@reflection.through_reflection.klass.descends_from_active_record?
121: 
122:         if association_conditions || through_conditions || source_conditions || uses_sti
123:           all = []
124: 
125:           [association_conditions, source_conditions].each do |conditions|
126:             all << interpolate_sql(sanitize_sql(conditions)) if conditions
127:           end
128: 
129:           all << through_conditions  if through_conditions
130:           all << build_sti_condition if uses_sti
131: 
132:           all.map { |sql| "(#{sql})" } * ' AND '
133:         end
134:       end
build_sti_condition() click to toggle source
     # File lib/active_record/associations/through_association_scope.rb, line 147
147:       def build_sti_condition
148:         @reflection.through_reflection.klass.send(:type_condition).to_sql
149:       end
build_through_conditions() click to toggle source
     # File lib/active_record/associations/through_association_scope.rb, line 136
136:       def build_through_conditions
137:         conditions = @reflection.through_reflection.options[:conditions]
138:         if conditions.is_a?(Hash)
139:           interpolate_sql(@reflection.through_reflection.klass.send(:sanitize_sql, conditions)).gsub(
140:             @reflection.quoted_table_name,
141:             @reflection.through_reflection.quoted_table_name)
142:         elsif conditions
143:           interpolate_sql(sanitize_sql(conditions))
144:         end
145:       end
conditions() click to toggle source
     # File lib/active_record/associations/through_association_scope.rb, line 111
111:       def conditions
112:         @conditions = build_conditions unless defined?(@conditions)
113:         @conditions
114:       end
Also aliased as: sql_conditions
construct_conditions() click to toggle source

Build SQL conditions from attributes, qualified by table name.

    # File lib/active_record/associations/through_association_scope.rb, line 21
21:       def construct_conditions
22:         table_name = @reflection.through_reflection.quoted_table_name
23:         conditions = construct_quoted_owner_attributes(@reflection.through_reflection).map do |attr, value|
24:           "#{table_name}.#{attr} = #{value}"
25:         end
26:         conditions << sql_conditions if sql_conditions
27:         "(" + conditions.join(') AND (') + ")"
28:       end
construct_from() click to toggle source
    # File lib/active_record/associations/through_association_scope.rb, line 44
44:       def construct_from
45:         @reflection.table_name
46:       end
construct_join_attributes(associate) click to toggle source

Construct attributes for :through pointing to owner and associate.

     # File lib/active_record/associations/through_association_scope.rb, line 94
 94:       def construct_join_attributes(associate)
 95:         # TODO: revisit this to allow it for deletion, supposing dependent option is supported
 96:         raise ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(@owner, @reflection) if [:has_one, :has_many].include?(@reflection.source_reflection.macro)
 97: 
 98:         join_attributes = construct_owner_attributes(@reflection.through_reflection).merge(@reflection.source_reflection.primary_key_name => associate.id)
 99: 
100:         if @reflection.options[:source_type]
101:           join_attributes.merge!(@reflection.source_reflection.options[:foreign_type] => associate.class.base_class.name.to_s)
102:         end
103: 
104:         if @reflection.through_reflection.options[:conditions].is_a?(Hash)
105:           join_attributes.merge!(@reflection.through_reflection.options[:conditions])
106:         end
107: 
108:         join_attributes
109:       end
construct_joins(custom_joins = nil) click to toggle source
    # File lib/active_record/associations/through_association_scope.rb, line 53
53:       def construct_joins(custom_joins = nil)
54:         polymorphic_join = nil
55:         if @reflection.source_reflection.macro == :belongs_to
56:           reflection_primary_key = @reflection.klass.primary_key
57:           source_primary_key     = @reflection.source_reflection.primary_key_name
58:           if @reflection.options[:source_type]
59:             polymorphic_join = "AND %s.%s = %s" % [
60:               @reflection.through_reflection.quoted_table_name, "#{@reflection.source_reflection.options[:foreign_type]}",
61:               @owner.class.quote_value(@reflection.options[:source_type])
62:             ]
63:           end
64:         else
65:           reflection_primary_key = @reflection.source_reflection.primary_key_name
66:           source_primary_key     = @reflection.through_reflection.klass.primary_key
67:           if @reflection.source_reflection.options[:as]
68:             polymorphic_join = "AND %s.%s = %s" % [
69:               @reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type",
70:               @owner.class.quote_value(@reflection.through_reflection.klass.name)
71:             ]
72:           end
73:         end
74: 
75:         "INNER JOIN %s ON %s.%s = %s.%s %s #{@reflection.options[:joins]} #{custom_joins}" % [
76:           @reflection.through_reflection.quoted_table_name,
77:           @reflection.quoted_table_name, reflection_primary_key,
78:           @reflection.through_reflection.quoted_table_name, source_primary_key,
79:           polymorphic_join
80:         ]
81:       end
construct_owner_attributes(reflection) click to toggle source

Construct attributes for associate pointing to owner.

    # File lib/active_record/associations/through_association_scope.rb, line 84
84:       def construct_owner_attributes(reflection)
85:         if as = reflection.options[:as]
86:           { "#{as}_id" => @owner.id,
87:             "#{as}_type" => @owner.class.base_class.name.to_s }
88:         else
89:           { reflection.primary_key_name => @owner.id }
90:         end
91:       end
construct_quoted_owner_attributes(reflection) click to toggle source

Associate attributes pointing to owner, quoted.

    # File lib/active_record/associations/through_association_scope.rb, line 31
31:       def construct_quoted_owner_attributes(reflection)
32:         if as = reflection.options[:as]
33:           { "#{as}_id" => owner_quoted_id,
34:             "#{as}_type" => reflection.klass.quote_value(
35:               @owner.class.base_class.name.to_s,
36:               reflection.klass.columns_hash["#{as}_type"]) }
37:         elsif reflection.macro == :belongs_to
38:           { reflection.klass.primary_key => @owner.class.quote_value(@owner[reflection.primary_key_name]) }
39:         else
40:           { reflection.primary_key_name => owner_quoted_id }
41:         end
42:       end
construct_scope() click to toggle source
    # File lib/active_record/associations/through_association_scope.rb, line 8
 8:       def construct_scope
 9:         { :create => construct_owner_attributes(@reflection),
10:           :find   => { :conditions  => construct_conditions,
11:                        :joins       => construct_joins,
12:                        :include     => @reflection.options[:include] || @reflection.source_reflection.options[:include],
13:                        :select      => construct_select,
14:                        :order       => @reflection.options[:order],
15:                        :limit       => @reflection.options[:limit],
16:                        :readonly    => @reflection.options[:readonly],
17:            } }
18:       end
construct_select(custom_select = nil) click to toggle source
    # File lib/active_record/associations/through_association_scope.rb, line 48
48:       def construct_select(custom_select = nil)
49:         distinct = "DISTINCT " if @reflection.options[:uniq]
50:         selected = custom_select || @reflection.options[:select] || "#{distinct}#{@reflection.quoted_table_name}.*"
51:       end
sql_conditions() click to toggle source
Alias for: conditions

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.