Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql joins order by isn't including the table name, causes ambiguous column error when using select or pluck #44

Open
sr75 opened this issue May 30, 2013 · 0 comments

Comments

@sr75
Copy link

sr75 commented May 30, 2013

When you join tables the ranked model doesn't include table alias in the order by so you get error:

Column 'row_order' in order clause is ambiguous

You only get this bug when attempting to access columns from result using pluck or select etc...

model.rb
include RankedModel
ranks :track_order, column: :row_order

## scopes ##
default_scope rank(:track_order)

Cause sql gen'd for:

Track.joins(:videos).where(videos: {fan_id: fan_id, workflow_state: [:submitted, :pending, :approved]}).pluck(:id)
SELECT `tracks`.`id` FROM `tracks` INNER JOIN `videos` ON `videos`.`track_id` = `tracks`.`id` WHERE `videos`.`fan_id` = ? AND `videos`.`workflow_state` IN ('submitted', 'pending', 'approved') ORDER BY row_order

Was able to override by doing this on include:

# model.rb
include RankedModel
ranks :track_order, column: 'tracks.row_order'

## scopes ##
default_scope rank(:track_order)

The above change generates sql correctly:

SELECT `tracks`.`id` FROM `tracks` INNER JOIN `videos` ON `videos`.`track_id` = `tracks`.`id` WHERE `videos`.`fan_id` = ? AND `videos`.`workflow_state` IN ('submitted', 'pending', 'approved') ORDER BY tracks.row_order

Gem should probably always alias order by clauses just to be safe of possible ambiguous column errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant