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

Allow sorting using generated/virtual columns [Postgres] #1473

Open
Tobias-Knudsen opened this issue Jan 9, 2024 · 0 comments
Open

Allow sorting using generated/virtual columns [Postgres] #1473

Tobias-Knudsen opened this issue Jan 9, 2024 · 0 comments

Comments

@Tobias-Knudsen
Copy link

We're using Ransack in our Rails API. One of our Postgres tables has generated/virtuals columns, however they aren't included in the Ransack query, when I try and sort by these columns:

Here is my migration for adding the virtual column:

class AddGeneratedColumnsToVehicles < ActiveRecord::Migration[7.1]
  def change
    # Add virtual column to calculate horsepower
    add_column :vehicles, :horsepower, :virtual, type: :float, as: "ROUND((engine_power / 0.745699872)::numeric, 2)::float", stored: true
  end
end

Example:

ransack = Vehicle.ransack
ransack.sorts = ['horsepower DESC']
ransack.result.to_sql 
# OUTPUT: "SELECT "vehicles".* FROM "vehicles"

I would expect the output to be:
"SELECT "vehicles".* FROM "vehicles" ORDER BY "vehicles"."horsepower" DESC

https://www.postgresql.org/docs/current/ddl-generated-columns.html

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