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

Two typos in the doc #468

Closed
v5out opened this issue Nov 10, 2014 · 24 comments
Closed

Two typos in the doc #468

v5out opened this issue Nov 10, 2014 · 24 comments

Comments

@v5out
Copy link

v5out commented Nov 10, 2014

Ransack is great! Cleaning up a few typos in the doc would help a bit, too. The two that got me today were:

<%= content_tag :th, sort_link(@q, 'departments.title') %>
<%= content_tag :th, sort_link(@q, 'employees.last_name') %>

I believe those should be _ (underscores) instead of "." and supervisor belongs_to department so that should be 'department_title'.

@jonatack
Copy link
Contributor

@v5out thanks! For the code, I believe the doc is correct as is. However, in the console, underscores replace the period. See #431 and #450. It's true that it is confusing. If the parsing could be made to work with underscores everywhere, that could be a useful contribution.

Could you let me know in what situation (code or console, Rails/Ruby/Ransack versions) you came across this?

@v5out
Copy link
Author

v5out commented Nov 11, 2014

Thanks Jon, had not seen those issues and, like the others, didn't know about [skip ci]

'department_title' format works for me in the code, have not tried the console. If I use the 'department.title' format there's no error, the sort just doesn't work.

Rails 4.1.5, Ruby 2.1.2, Ransack 1.4.1 (For completeness...using Kaminari 0.16.1 for pagination and Draper 1.4.0 for decorators.)

My index.html.erb file looks like:

<%= sort_link @q, 'folio_name', "Folio name" %>

@jonatack
Copy link
Contributor

Thanks. With the period you would need to use departments.title (not department.title), does it work for you then? What kind of association is involved? It would be good to resolve this.

@v5out
Copy link
Author

v5out commented Nov 11, 2014

No, 'departments.title' doesn't work either, no error, just doesn't sort.

The associations I'm using are similar to the doc but are a bit different, as follows. Perhaps it is the "through" association that makes the difference?

class Company < ActiveRecord::Base
  has_many :departments, :dependent => :destroy
end

class Department < ActiveRecord::Base
  belongs_to :company

  has_many :supervisors, :dependent => :destroy
  has_many :managers, through :supervisors
end

class Supervisor < ActiveRecord::Base
  belongs_to :department
  belongs_to :manager
end

class Manager < ActiveRecord::Base
  has_many :supervisors
end

@dgm
Copy link

dgm commented Jan 27, 2015

As I commented on #13, the documentation is wrong. the sort link string appear to not map directly to sql. It works as department_title but not as departments.title or department.title

@jonatack
Copy link
Contributor

@dgm I am unable to reproduce this. Could you please provide the information mentioned in the Contributing Guide please?

@dgm
Copy link

dgm commented Jan 27, 2015

Here's a basic rails app that implements the documentation.

https://github.com/dgm/ransack-sort-demo

As you can see, the links as documented do not work, but I provide other links that do.

The basic misconception seems to be in thinking that 'the association is expressed as an SQL string' ... It most assuredly is not. It uses the same association name underscore attribute format as all the rest of the searchers. (minus a predicate like _cont).

@jonatack
Copy link
Contributor

Could you begin by providing the basic info in the Contributing Guide so that we can try to reproduce your environment? It looks like you are using Rails 4.2 in the demo app, but no version of Ruby or Ransack is specified and it is not practical to have to guess. I'm not saying you're wrong, but I have to reproduce your environment and issue.

@dgm
Copy link

dgm commented Jan 27, 2015

The Gemfile and Gemfile.lock should have all that info.

Rails 4.2, ruby 2.1 or ruby 2.2 , it won't matter.

ransack (1.5.1)

@jonatack
Copy link
Contributor

I don't see the Ruby version specified in the demo app.

Are you encountering the issue with the latest Ransack release (1.6.3)?

@dgm
Copy link

dgm commented Jan 27, 2015

Gemfile.lock always has that info for rails apps. Anyway, no change on 1.6.3

@dgm
Copy link

dgm commented Jan 27, 2015

And I don't think there should be any change - the current behavior is correct, it uses the existing association to generate the sort - which may include special conditions into the overall sql. sorting on a simple sql table.column is way too simplistic for what ransack actually does. The documentation is just wrong.

@jonatack
Copy link
Contributor

How are you testing this?

@dgm
Copy link

dgm commented Jan 27, 2015

I meant to commit the sqlite database with that so the test data is there... pull the latest changes, run the rails server and look at http://localhost:3000/supervisors

@AdamFerguson
Copy link

@dgm Thanks, I wasn't able to get sort_link's working on associations according to the documentation using the 'plural_table_name.column_name' convention but using the 'association_column' convention you suggested worked.

@jacwellington
Copy link

@dgm I experienced the same behavior in 1.6.6. I needed to do 'association_column' to get it to work. The way the docs say to do it doesn't work.

@UlyssesInvictus
Copy link

I know it's pouring oil on the fire at this point, but I experienced the same behavior with 1.7.0. The SQL doesn't work at all, in any form or pluralization, but a symbolized underscore works great.

@jonatack
Copy link
Contributor

Thanks @UlyssesInvictus, could you provide actionable information to reproduce? Version of Active Record, Ruby, which database? Are you using Ransack via another gem like Active Admin? On the command line or in your app?

@jonatack
Copy link
Contributor

The original issue is #13. I'd like to understand how/when people are having this issue. In the meantime, I've committed a change in the docs that provides both versions.

@dgm
Copy link

dgm commented Aug 21, 2015

As I see it, it is not an sql string at all, but the association, just like "Ransack objects elsewhere" as the removed part of your commit above said... if the association is a belongs_to, it will be singular, and underscores to seperate parts.... just like ransack objects elsewhere

@jonatack
Copy link
Contributor

I agree that it should be like that, but have not been able to reproduce... only SQL-style strings work for sort links on associations for me. I don't use Active Admin or Simple Form -- all standard omakase Rails. Perhaps I'm doing something in my code that no one else is doing 🌵

This afternoon I have to step through the sort link code with byebug/pry to work on Ransack breaking with Rails master now that strong parameters inherit from ActionController::Parameters now. Lots of things are breaking from this... it's pretty bad. Perhaps I'll see what's going on with the associations while I'm at it. Otherwise will look through your example app above.

@UlyssesInvictus
Copy link

Oh, sorry, forgot many crucial details--that's so odd that it breaks for you in the opposite way it breaks for everyone else.

Anyways, I was running:

  • Ruby 2.2.2
  • Rails (and ActiveRecord) 4.2.3
  • MySQL
  • in both the console and directly from the view/controller
  • Ransack by itself (though I can't speak for its own dependencies, since I just naively installed it with bundler)
  • A basic structure of Model A belongs_to Model B, where I wanted to sort_link query, :b_attribute, where query was created with a_instances.ransack. :b_attribute works, but 'bs.attribute' and 'b.attribute' both fail silently. (I also made sure I was .includes(:b)-ing every time.)

@jonatack
Copy link
Contributor

Thanks. Not sure it is the opposite for everyone else, 2 million downloads of Ransack (1 million in the past 12 months) and ten people mention a problem. It could have been people using Active Admin (which modifies the Ransack API) or using another database like Sequel instead of Active Record. But I'm willing to believe it might be something on my side until I really go through it.

@UlyssesInvictus
Copy link

No, of course--should have clarified "everyone else [in this thread]."

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

6 participants