Skip to content

Commit

Permalink
Include table name in queries to avoid ambiguous column errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
schneidmaster committed May 1, 2017
1 parent 247be5d commit e0f9b84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/surus/json/belongs_to_scope_builder.rb
Expand Up @@ -4,10 +4,18 @@ class BelongsToScopeBuilder < AssociationScopeBuilder
def scope
s = association
.klass
.where("#{quote_column_name association.active_record_primary_key}=#{quote_column_name association.foreign_key}")
.where("#{association_primary_key}=#{association_foreign_key}")
s = s.instance_eval(&association.scope) if association.scope
s
end

def association_primary_key
"#{association.quoted_table_name}.#{quote_column_name association.active_record_primary_key}"
end

def association_foreign_key
"#{outside_class.quoted_table_name}.#{quote_column_name association.foreign_key}"
end
end
end
end
2 changes: 1 addition & 1 deletion lib/surus/json/has_many_scope_builder.rb
Expand Up @@ -14,7 +14,7 @@ def outside_primary_key
end

def association_foreign_key
"#{connection.quote_column_name association.foreign_key}"
"#{association.quoted_table_name}.#{connection.quote_column_name association.foreign_key}"
end
end
end
Expand Down

0 comments on commit e0f9b84

Please sign in to comment.