malomalo / activerecord-sort Goto Github PK
View Code? Open in Web Editor NEWA safe way to accept user parameters and order against your ActiveRecord Models
License: MIT License
A safe way to accept user parameters and order against your ActiveRecord Models
License: MIT License
in knotel/knopi:
Deal.sort(assignments: {assigned_at: :desc}).distinct
>> ActiveRecord::StatementInvalid (PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ...N "assignments"."deal_id" = "deals"."id" ORDER BY "assignmen...
^
: SELECT DISTINCT "deals".* FROM "deals" INNER JOIN "assignments" ON "assignments"."deal_id" = "deals"."id" ORDER BY "assignments"."assigned_at" DESC LIMIT $1)
it looks like postgres requires assignments.assigned_at
to be in select.
If I modify sort_for_relation
in https://github.com/malomalo/activerecord-sort/blob/master/ext/active_record/base.rb#L85 and insert:
resource = resource.select(column)
resource = resource.order(Arel::Nodes::Descending.new(column, nulls))
It works as expected, but it breaks count:
Deal.sort(assignments: {assigned_at: :desc}).count
ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: syntax error at or near "Arel"
LINE 1: SELECT COUNT(#<struct Arel::Attributes::Attribute relation=#...
^
: SELECT COUNT(#<struct Arel::Attributes::Attribute relation=#<Arel::Table:0x00007fcf911f6630 @name="deals", @type_caster=#<ActiveRecord::TypeCaster::Map:0x00007fcf911f6680 @types=Deal(id: uuid, build_type: string, status: string, stage: string, address_street: string, address_city: string, address_state: string, address_submarket: string, address_metro: string, address_postal_code: string, address_country: string, lease_type: string, property_class: string, property_construction: string, property_amenities: string, passenger_elevators: integer, freight_elevators: integer, contact_commitment_conference_rooms: string, contact_commitment_flooring: string, contact_commitment_fronts: string, contact_commitment_ada: string, contact_commitment_pantry: string, contact_commitment_conference_rooms_explanation: text, contact_commitment_flooring_explanation: text, contact_commitment_fronts_explanation: text, contact_commitment_ada_explanation: text, contact_commitment_pantry_explanation: text, created_at: datetime, updated_at: datetime, qualified_at: datetime, term_sheet_signed_at: datetime, lease_signed_at: datetime, drive_url: string, inactive_reason: string, address_market: string, owner_organization: string, accounts_count: integer, tourable_on: date, possession_on: date, tour_instructions: text, occupancy: integer, market_rate: float, target_rate: float)>, @table_alias=nil>, name="*">, #<struct Arel::Attributes::Attribute relation=#<Arel::Table:0x00007fcf8fe9b720 @name="assignments", @type_caster=#<ActiveRecord::TypeCaster::Map:0x00007fcf8fe9b8b0 @types=Assignment(id: uuid, owner_id: uuid, completed_by_id: uuid, deal_id: uuid, title: string, stage: string, order: integer, completed_at: datetime, created_at: datetime, updated_at: datetime, disabled_at: datetime, assigned_at: datetime, snooze_to: datetime)>, @table_alias=nil>, name=:assigned_at>) FROM "deals" INNER JOIN "assignments" ON "assignments"."deal_id" = "deals"."id")
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.