Code Monkey home page Code Monkey logo

Comments (10)

glebm avatar glebm commented on May 28, 2024

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.

twelve17 avatar twelve17 commented on May 28, 2024

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.

glebm avatar glebm commented on May 28, 2024

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.

twelve17 avatar twelve17 commented on May 28, 2024

@glebm does calling after on that point work for you? I will come up with a replication bit of code.

from order_query.

glebm avatar glebm commented on May 28, 2024

The result of that is not a point but a Space. You need to call at to get a Point.

from order_query.

twelve17 avatar twelve17 commented on May 28, 2024

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.

glebm avatar glebm commented on May 28, 2024

Closing as it's old. In case you still need help, consider StackOverflow.

from order_query.

twelve17 avatar twelve17 commented on May 28, 2024

@glebm were you not able to replicate the problem with the gist I created?

from order_query.

glebm avatar glebm commented on May 28, 2024

I've had a look at your gist. It works if you simply:

  1. 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"])
  2. Define the contract_type method on Employee:

    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.

twelve17 avatar twelve17 commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.