-
Notifications
You must be signed in to change notification settings - Fork 209
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
Counter on polymorphic relationship #4
Comments
Mmm, that's a good question. I've never tested that so this might or might not work. Feel free to fork and pull-request any necessary changes to make that work. |
can I use this with has_and_belong_to_many? |
Again, I haven't tested that but I don't think so. Frankly, I wouldn't recommend has_and_belongs_to_many. It's usually better to just have a model sit in between. And then that way counter_culture also works fine. |
Hey guys, if anyone wants to use counter_culture for polymorphic relationships, check out my fork. I had to fix this for a project. I added some logic that detects if a relationship is polymorphic and traverses the respective models to find the caches. Check out my fork: https://github.com/dorilla/counter_culture I've only tested and confirmed the following:
The edits I had to do seems hacky to me, so I do plan of working on it some more when I have the time. Hope it helps. |
worked for me 👍 |
@dorilla -- thanks so much for sharing this and great to hear that it works for people. As you'd pointed out, the code is a little hack-ish right now so I wouldn't want to merge it back into the main gem yet. But it's definitely a great place to start. I've also added a link to this issue in the documentation so people can find your fork more easily. Thanks again for contributing! |
+1 - Would love to have this as a native feature of counter_culture |
@magnusvk is there any on-going work regarding this issue? |
No, I'm not working on it and I'm not aware of anybody else. Unfortunately, On Thu, Aug 15, 2013 at 8:52 AM, Emil Ahlbäck [email protected]:
|
@magnusvk Thanks for the reply. Could you change the link in the README so that it points to this repo instead of the old one? Will make it easier for people to find this issue. |
@mull Done -- thanks for pointing that out. |
@magnusvk As you mentioned 7 months ago, @dorilla's fork is "a little hackish" so you're not inclined to merge that work into your mainline. Well, I am also in need of this polymorphic capability, but I'm also interested in seeing the work incorporated here, for benefit to all. 😄 Anyway, I'm willing to spend the time to make that work less "hackish" but I'd like to know what you consider less to be. |
Well, I'd love to find a solution that is not hackish. :) Here's a couple of things:
This is probably not exhaustive but it's a solid amount of work to start. I looked into implementing this myself last week, but backed away a bit after seeing the complexity involved, particularly with multi-level support. Of course all this is possible, but it's a major undertaking, and I don't want to destabilize the non-polymorphic support we have right now. |
First I was like, "I have a big need and a perfect use case for polymorphic counter_culture. It would be so nice! Ok, I guess I'll use dorilla's fork." Then I was like, "Ok, I switched my Gemfile and did bundle update. I have the new fork. Wait, how do I even use the polymorphic capability. It's nowhere in the documentation." Then I was like, "Let me look at the source code." def all_polymorphic_types_currently(name)
@poly_hash ||= {}.tap do |hash|
Rails.application.eager_load! unless Rails.configuration.cache_classes
::ActiveRecord::Base.descendants.each do |reflection|
klass = (reflection rescue nil)
next if klass.nil? or !klass.ancestors.include?(::ActiveRecord::Base)
klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
(hash[reflection.options[:as]] ||= []) << klass
end
end
end
@poly_hash[name.to_sym]
end Then I was like, "... ... ... Whoa. Whoa Whoa Whoa. I can't even read this, let alone figure out what it does." Then I was like, "I wonder how out of date it is anyway." Then I was like, "Oh. 8 Months. I wonder if, since @magnusvk and I both live in NYC, if there wouldn't be some Saturday we could pair and just knock this motherfucker out." |
Justin -- maybe in the new year. :) Seriously, though, as I don't use |
Any work on this? |
@esbanarango, I have some code in my fork https://github.com/deniskorobicyn/counter_culture/tree/polymorphic-assosiations, but its not fully tested and not documented. If I'll have some time in next year, I'll finish this according with Some simple usage: #community.rb
class Community
# should have users_count column
has_many :users
end
#organization.rb
class Organization
# should have users_count column
has_many :users
end
#user.rb
class User
belongs_to :group, polymorphic: true
counter_culture :group
end
# new feature count only certain classes (not fully tested)
class User
belongs_to :group, polymorphic: true
counter_culture :group, only: [[:community]] # double brackets are required by now
end You can find other examples in P.S. |
@deniskorobicyn thank you for this! 👍 |
@deniskorobicyn thanks for the anti-pattern link. There are lot of useful information. |
any updates on this, would love to have polymorphic capability, as i have used this extensively in my project. |
Nothing new from my end, unfortunately. |
Hi, Are there any updates on this yet or newer forks with this capability? I used this gem extensively in a project and now need polymorphic support. Thanks! |
No unfortunately not.
|
#154 is a new(ish) PR for single-level polymorphic counters (I think multi-level may be possible, I just haven't come up with a reasonable test case yet). After a bit of too-ing and fro-ing I think this is working. If anyone has a use-case for multi-level polymorphic counters can you add a simplified example here -- it would help motivate a subsequent multi-level polymorphic counter PR. |
Closing this as one-level polymorphic relationships are supported now as of version 1.5.0. Thank you @timdiggins for making this happen. |
How to make counter on polymorphic relationship?
The text was updated successfully, but these errors were encountered: