brendon / ranked-model

An acts_as_sortable/acts_as_list replacement built for Rails 4+
https://github.com/mixonic/ranked-model
MIT License
1.09k stars 134 forks source link

Invalid SQL in `relative_rank` when order column value is nil #177

Closed mattvague closed 3 months ago

mattvague commented 3 years ago

If you have a model with nil as the position in the database, calling model. branch_order_rank will raise an error due to invalid SQL, eg:

ActiveRecord::StatementInvalid Exception: PG::SyntaxError: ERROR:  syntax error at or near ")"
LINE 1: ... AND "relationship_nodes"."id" != $3 AND ("branch_order" < )

I think I've narrowed down this issue to this method.I know that ranked-model attempts to always populate the column on save but it's possible if you have an existing dataset, bypass callbacks, that you could still have nil values making this an issue.

Not sure what the right solution is, but I feel like this situation should be gracefully handled somehow. Happy to open a PR with a little guidance. Thanks!

brendon commented 3 years ago

Hi @mattvague, I'm not too sure what to do about this. It looks like you're explicit calling branch_order_rank on this item? Perhaps you could check if the branch_order is nil first?

If that's not the code path then I'd be keen to know what it is to see if we can cope.

My suggestion would be to add mechanisms on your end to keep your data clean (if it has a tendency to get out of order). If you don't bypass the callbacks and ensure any existing data is fixed up before using ranked-model you should have a better time :)

brendon commented 3 months ago

No further feedback, so closing this issue.