Comments (10)
What do you want to achieve? It's a has_many association, so which one of the many do you want it to sort by? Max? Lexicographic?
from order_query.
Apologies, I probably should have used a has_one
in the example. Thus it would be a one-to-one join of two tables, sorting by a field in the second table.
from order_query.
For the has_one
case this should be sufficient:
point = Sandwich.joins(:topping).seek([:topping_name, :desc, sql: 'toppings.name'])
class Sandwich < ...
def topping_name
topping.name
end
end
from order_query.
@glebm does calling after
on that point
work for you? I will come up with a replication bit of code.
from order_query.
The result of that is not a point but a Space
. You need to call at
to get a Point
.
from order_query.
Apologies for the delay. Indeed, once I get a Point
, calling after
on it is where I get the error. I've created a rails spec gist to replicate the error.
The spec creates models Company
and Employee
, the latter is a has_one
association of the former. The spec is designed to connect to a mysql database (in my case, version 5.7.19) via these environment variables:
DB_HOST
DB_USERNAME
DB_PASSWORD
DB_NAME
- optional:
DB_PORT
(defaults to 3306)
The test will create the database specified by DB_NAME
, create the table structure, then seed the data with a few records: two Employee
records, each with a Contract
associated with it. Then it runs a test where it defines a space
with the sorting on the association, then finds the point
at the first record, then calls after
to get one record after.
This is the result of the test I'm encountering:
$ DB_HOST=127.0.0.1 DB_USERNAME=<X> DB_PASSWORD=<Y> DB_NAME=order_query rspec spec/association_spec.rb
...
1) OrderQuery search sort on secondary join table can fetch 'next' on a Employee point when sorting by contract.contract_type
Failure/Error: record = space.at(Employee.first).after(false).limit(1)
NoMethodError:
undefined method `contract_type' for #<Employee id: 1, name: "John", email: "[email protected]">
Did you mean? contract
# ./spec/association_spec.rb:145:in `block (4 levels) in <top (required)>'
Finished in 2.91 seconds (files took 0.89039 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/association_spec.rb:141 # OrderQuery search sort on secondary join table can fetch 'next' on a Employee point when sorting by contract.contract_type
I feel like I must be doing something wrong. Help is appreciated!
from order_query.
Closing as it's old. In case you still need help, consider StackOverflow.
from order_query.
@glebm were you not able to replicate the problem with the gist I created?
from order_query.
I've had a look at your gist. It works if you simply:
-
Pass the correct arguments to order_query
-space = Employee.joins(:contract).seek([:contract_type, :desc, "contract.contract_type"]) +space = Employee.joins(:contract).seek([:contract_type, :desc, sql: "contract.contract_type"])
-
Define the
contract_type
method onEmployee
:delegate :contract_type, to: :contract
There is an example of using sql:
in the Readme: https://github.com/glebm/order_query#advanced-example
from order_query.
Hi @glebm , I apologize for not responding to this sooner. I re-reviewed this today and now understand your approach, and also have been able to reproduce it locally. Thanks again for your help!
from order_query.
Related Issues (20)
- Ordering by NULLS FIRST / LAST HOT 10
- documentation on how to "paginate" results HOT 6
- new version release HOT 1
- Test on multiple databases
- pass table and column to custom sql function HOT 11
- Nulls ordering skips records if there is more than one NULL HOT 6
- TODO: Better tests for nils in enums
- Change base scope? HOT 2
- Get point for reverse scope HOT 2
- Rails 6 support HOT 2
- Wrong order? HOT 1
- Top-level disjunctive causes sub-optimal query plan HOT 6
- Calling prev and next when the ordering model attribute is enumerable creates an invalid SQL where clause HOT 2
- Redundant condition elimination
- #seek or #at with previously specified order HOT 5
- Can this sort on nested attributes? HOT 3
- Feature Request: HOT 1
- .previous always showing first record when limiting scope HOT 2
- Not compatible with rails 5 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from order_query.