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

AmbiguousColumn ERROR in views/madmin/fields/has_many/_form.html.erb #160

Open
dbreunig opened this issue Dec 8, 2022 · 0 comments
Open

Comments

@dbreunig
Copy link

dbreunig commented Dec 8, 2022

When attempting to edit a record whose model is associated with another model, I'm hitting the following error:

Showing ruby/3.1.0/lib/ruby/gems/3.1.0/gems/madmin-1.2.7/app/views/madmin/fields/has_many/_form.html.erb where line #4 raised:
PG::AmbiguousColumn: ERROR: column reference "created_at" is ambiguous
LINE 1: ..."."list_id" WHERE "stars"."user_id" = $1 ORDER BY created_at...

The issue is pointing at that created_at. Then it cites line 768:

@connection.exec_params(sql, type_casted_binds)

The sql var passed into the exec_params method is:

SELECT lists.id FROM lists INNER JOIN stars ON lists.id = stars.list_id WHERE stars.user_id = $1 ORDER BY created_at DESC

This query is coming from an association on the List model, which reads:

class User < ApplicationRecord
    # ...
    has_and_belongs_to_many :starred_lists, -> { order "created_at DESC" }, join_table: "stars", class_name: "List", foreign_key: "user_id", association_foreign_key: "list_id", inverse_of: "starring_users"
    # ...
end

I was able to fix this issue on my end by updating the order clause, like so:

class User < ApplicationRecord
    # ...
    has_and_belongs_to_many :starred_lists, -> { order "stars.created_at DESC" }, join_table: "stars", class_name: "List", foreign_key: "user_id", association_foreign_key: "list_id", inverse_of: "starring_users"
    # ...
end

Given the previous association definition had not caused any issues throughout the rest of my app, you may consider this an issue to address. If you don't, makes sense: I'm also putting this hear for any future Googlers.

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