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

Scope for Polymorphic association + ManyToMany #106

Closed
USvER opened this issue Feb 9, 2014 · 12 comments
Closed

Scope for Polymorphic association + ManyToMany #106

USvER opened this issue Feb 9, 2014 · 12 comments

Comments

@USvER
Copy link

USvER commented Feb 9, 2014

I have this setup: "Topic" and "Comment" has many "Assets" through polymorphic join table.
I want my topics and comments to have their assets ordered. To make my act_as_list work for Topics and Comments separately it should use default scope. How to do that?

@USvER
Copy link
Author

USvER commented Feb 9, 2014

Hm... i guess i need to define position on the join model itself for that to work...

@swanandp
Copy link
Contributor

I think the gem should support lambdas for scopes. With that, you can just define your own scope.

@USvER
Copy link
Author

USvER commented Feb 23, 2014

Polimorphic relation(or maybe all relations) is scopes by itself. So i have defined polimorphic relation as "holder". So asset has holder, wich can be either Topic or Comment... This way i just do

belongs_to :holder, polymorphic: true
acts_as_list scope: :holder

And all works... Still lambdas may be a good idea = )
Thank you!

@firedev
Copy link

firedev commented Apr 13, 2014

I use it the same way:

belongs_to :imageable, polymorphic: true
acts_as_list scope :imageable

However Rails 4 complaints:

DEPRECATION WARNING: Calling #scope or #default_scope with a hash is deprecated. Please use a lambda containing a scope. E.g. scope :red, -> { where(color: 'red') }.

What would be the right syntax here?

@firedev
Copy link

firedev commented Apr 14, 2014

Even worse Rails 4.1 simply rejects it:

`scope': wrong number of arguments (0 for 2) (ArgumentError)

Suggestions?

@firedev
Copy link

firedev commented Apr 14, 2014

Anyway, I did it like this:

  acts_as_list scope :imageable, -> { includes :imageable }

@firedev
Copy link

firedev commented Jun 6, 2014

Okay, upon further investigation I have figured out that it simply doesn't work. It reorders all images ignoring the scope. Switched to ranked-model. Took a few minutes.

@firedev
Copy link

firedev commented Aug 28, 2014

Probably worth checking, however this is for Rails 3 http://jakeboxer.com/blog/2011/10/09/using-acts-as-list-in-a-polymorphic-scope/

class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
  acts_as_list scope: [:imageable_id, :imageable_type]
end

@USvER
Copy link
Author

USvER commented Aug 28, 2014

@firedev hm, strange... i didn't had this issue... just used as usual scope...

@Feuda
Copy link

Feuda commented Sep 30, 2014

@firedev

acts_as_list scope: [:imageable_id, :imageable_type]

This worked in rails4!

@firedev
Copy link

firedev commented Oct 1, 2014

Guess this was fixed since.

@swanandp
Copy link
Contributor

swanandp commented Oct 1, 2014

Closing.

@swanandp swanandp closed this as completed Oct 1, 2014
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

4 participants