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

Ranking with_same through association #89

Closed
willcosgrove opened this issue Dec 13, 2014 · 5 comments
Closed

Ranking with_same through association #89

willcosgrove opened this issue Dec 13, 2014 · 5 comments

Comments

@willcosgrove
Copy link

Here's my setup

# area.rb
class Area < ActiveRecord::Base
  has_many :tasks
end

# task.rb
class Task < ActiveRecord::Base
  belongs_to :area
  has_many :project_tasks
end

# project_task.rb
class ProjectTask < ActiveRecord::Base
  belongs_to :task
  belongs_to :project # not relevant for this example, for for completeness sake
  has_one :area, through: :task

  ranks :area_order, with_same: :area_id # Doesn't work because there's not an area_id column on project_tasks
end

I'm not sure how to solve this. I could cache area_id on the project_task, but that seems messy, and I'd then have to deal with making sure that always stays in sync. Is there anyway to use RankedModel's with_same feature with a joined attribute?

Also, thanks for making this awesome gem! It's saved me tons of time.

@willcosgrove willcosgrove changed the title Ranking with same through association Ranking with_same through association Dec 13, 2014
@brendon
Copy link
Owner

brendon commented Jun 4, 2024

Closing this for now. Perhaps there would be a way if the default scope included a join? You've probably moved on after 10 years :D

@brendon brendon closed this as completed Jun 4, 2024
@yannickgloster
Copy link

This is something I'm looking to figure out as well, it doesn't look like adding a default scope with the join affects the query that is being produced.

@brendon
Copy link
Owner

brendon commented Jun 4, 2024

True, as I think we unscope on purpose to keep our queries clean. I could imagine opening things up to scope could get out of hand pretty quickly. Still, would be an interesting exercise. Perhaps it'd be simpler to try it out on my other gem that has a much more straightforward codebase: https://github.com/brendon/positioning?

@brendon
Copy link
Owner

brendon commented Jun 4, 2024

Perhaps a virtual column could work?

@yannickgloster
Copy link

I'll take a look at the other gem. I think realistically for this, I'll end up copying the ID on the parent to the child and keeping them in sync with callbacks.

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

3 participants