Every repository with this icon (
Every repository with this icon (
| Description: | An ActiveRecord plugin for self-referential and double-sided polymorphic associations. edit |
-
polymorphic associations over different Namespaces - not possible?!
5 comments Created 6 months ago by mugwumpI have the following situation:
class Housing::Kennel < ActiveRecord::Base
has_many_polymorphs :guests, :from => [:dogs,:cats,_birds] endclass GuestKennel < ActiveRecord::Base
belongs_to :kennel belongs_to :guest, :polymorphic => true endclass Pets::Dog < ActiveRecord::Base
endWhich I cannot get to run: (ActiveRecord::Associations::PolymorphicError: Could not find a valid class for :guests_kennels (tried GuestsKennel). If it's namespaced, be sure to specify it as :"guests/guests_kennels" instead.) - the error-message varies, depending in which module Kennel, GuestsKennel, Dog are put.
In the end, putting all three classes in one module and adding this module with :namespace => :housingandpets solves this issue.
Is there a way to specifiy the module for all three particpiants (Kennel, GuestsKennel, Dog) independently from each other and put them into different modules?!
(and thx for hmp anyway, works great!)
Comments
-
I'm trying to define multiple polymorphic relations (
has_many_polymorphs plugin) from a single parent to same children.Note has many viewers
Note has many editors
Viewers could be either Users or Groups
Editors could be either Users or Groups
Permission is the association model withnote_id,viewer_id,viewer_type,editor_id,editor_typefieldsEverything works out as expect as long as I have only one has_many_polymorphs relation defined in Note model
class User < ActiveRecord::Base; end class Group < ActiveRecord::Base; end class Note < ActiveRecord::Base has_many_polymorphs :viewers, :through => :permissions, :from => [:users, :groups] end class Permission < ActiveRecord::Base belongs_to :note belongs_to :viewer, :polymorphic => true end Note.first.viewers << User.first # => [#<User id: 1, ....>] Note.first.viewers << Group.first # => [#<User id: 1, ....>, #<Group ...>] Note.first.viewers.first # => #<User ....> Note.first.viewers.second # => #<Group ....>Now, problems start to appear when I add the second relation
class Note < ActiveRecord::Base has_many_polymorphs :viewers, :through => :permissions, :from => [:users, :groups] has_many_polymorphs :editors, :through => :permissions, :from => [:users, :groups] end class Permission < ActiveRecord::Base belongs_to :note belongs_to :viewer, :polymorphic => true belongs_to :editor, :polymorphic => true end Note.first.viewers << User.first # => [#<User id: ....>] # >>>>>>>> Note.first.editors << User.first NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.constantize ... vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/base.rb:18:in `instantiate'I've tried refining the definition of
has_many_polymorphsbut it didn't work. Not even with an STI model forViewPermission < Permission, andEditPermission < Permission.Any thoughts / workarounds / issue pointers are appreciated.
Rails 2.3.0
Comments
tamersalama
Thu Jun 11 12:55:39 -0700 2009
| link
Posted on StackOverflow as well - http://stackoverflow.com/questions/983225/multiple-hasmanypolymorphs-in-one-model
-
tagged_with_any: Is this tested? Supposed to work?
3 comments Created 4 months ago by mugwumpThe tagging-extension definse two additional finders on the model, tagged_with and tagged_with_any - the code for tagged_with any (in tagging_extensions.rb) reads like this:
def self.tagged_with_any(*tag_list)
Shouldnt't this be
def tagged_with_any(*tag_list)With the self. I get an "undefined method" but even without the self, I get a
Association named 'from' was not found; perhaps you misspelled it? (ActiveRecord::ConfigurationError)Is this supposed to work, is it used/tested somewhere? Just asking, before I dive deeper into this and try to fix it...
thx&cheers
StefanP.S: Is this the right place for communication anyway? It doesn't look like anybody is looking into these issues ever, should I go ask somewhere else?!
Comments
ok, I didn't do any thorough tests, only superficially fixed it:
remove the self, and add a :joins to to the options like this:
add_joins!(sql, options[:joins], scope)
With this changes, the method works, although it needs a little more testing...
-
association.include?(associated) fails oddly
1 comment Created 4 months ago by jamesarosenI have a owner model:
class Person < ActiveRecord::Base has_many_polymorphs :things, :from => [ :books, :cds, :dvds ], :through => :person_things enda relationship model:
class PersonThing < ActiveRecord::Base belongs_to :person belongs_to :thing, :polymorphic => true endand several owned models:
class Book < ActiveRecord::Base end ...If I create a relationship:
person = Person.create!(:name => 'Barney') book = Book.create!('Dinosaurs for Dummies') person.things << bookthen
include?fails even whenselectdoesn't:person.things.include? book # => false person.things.select { |t| t == book }.any? # => trueI'm guessing
include?is redefined on this enumeration, but it's not redefined correctly.Comments
-
ActionView::TemplateError: Mysql::Error in production environment
1 comment Created 3 months ago by ihunterWe're getting an interesting situation where a specific series of relationships produces an unknown column error in production only.
Consider the following:
class User < ActiveRecord::Base has_many_polymorphs :accessible_products, :through => :accessibles, :as => :contact, :foreign_key => 'contact_id', :foreign_type_key => 'contact_type', :polymorphic_key => 'owner_id', :polymorphic_type_key => 'owner_type', :from => [:"product/one",:"product/two"] end class Product < ActiveRecord::Base has_many_polymorphs :accessors, :through => :accessibles, :as => :owner, :foreign_key => 'owner_id', :foreign_type_key => 'owner_type', :polymorphic_key => 'contact_id', :polymorphic_type_key => 'contact_type', :from => [:emails, :users] end class Accessible < ActiveRecord::Base acts_as_double_polymorphic_join :owners =>[:"product/one",:"product/two"], :contacts => [:users, :emails] endThis cyclical relationship should be handled by the acts_as_double_polymorphic_join but it's erroring in production, possibly due preloading of models and caching them, not sure.
The error we receive in production is ActionView::TemplateError: Mysql::Error: Unknown column 'accessibles.accessible_product_id'
The foreign and polymorphic keys are clearly defined, not sure why this is happening and only in production.
Comments
-
Would it be possible to get a tagged revision that works with 2.1.2? We have a rather slow-to-upgrade rails code base and I love this plugin in my other projects. Thanks if it's possible!
Comments
-
commit bacaeb095f..., 'fix invalid byte sequence...' breaks gem on ruby 1.8.x
1 comment Created 2 months ago by stephenebThe new code in question is:
open(filename, :encoding=>"utf-8") do |file|the error reported is:
open-uri.rb:32:in `initialize': can't convert Hash into String (TypeError)The same error occurs using File.open:
File.open(filename, :encoding=>"utf-8") do |file|Presumably this only works in Ruby 1.9. I'm building Ruby 1.9 now to check. In the latest Pickaxe book this is the method described for setting the encoding to utf-8 when using File.open:
File.open(filename, 'r:utf-8')
This method also doesn't work in 1.8.x
Also see comments on this commit:
http://github.com/fauna/has_many_polymorphs/commit/bacaeb095f5e8cf7427679b2755680ab24f8503e
Comments
reverted in this commit in a fork:
http://github.com/stepheneb/has_many_polymorphs/commit/fd531f3310b2fbcf06a9e46e7fe55d71f5585ae3
-
Impossible to create an association with no polymorphic side.
0 comments Created 2 months ago by rurounijonesI am using ACL ( http://github.com/rurounijones/acl9/tree/master ) for access control and combining it with has_many_polymorphs for easy associations.
It is working great with one problem. ACL9 allows you to assign roles on objects,Classes or nothing. When I assign a role on an Object or Class then everything works fine (authorizable_type is not nil in these cases). However when I add a global role ( where authorizable_type is nil) I get the following error.
The error occurred while evaluating nil.constantize from (eval):7:in `before_save' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:347:in `send' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:347:in `callback' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:249:in `create_or_update' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2539:in `save_without_validation' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:1009:in `save_without_dirty' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/dirty.rb:79:in `save_without_transactions' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:229:in `send' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:229:in `with_transaction_returning_status' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:182:in `transaction' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:228:in `with_transaction_returning_status' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:196:in `save' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:208:in `rollback_active_record_state!' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:196:in `save' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:242:in `create' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:410:in `create_record' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:428:in `add_record_to_target_with_callbacks' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:410:in `create_record' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:240:in `create'I believe the error lies in this line:
def association_class @owner[@reflection.options[:foreign_type]] ? @owner[@reflection.options[:foreign_type]].constantize : nil endIs it possible to change this line to allow for associations where the polymorphic side is nil? In effect ignoring the :through aspect of the association record if the polymorphic side is nil and just leave it to the normal has_many.
Regards
Jeff Jones
Classes follow:
class User < ActiveRecord::Base has_many :roles has_many_polymorphs :authorizables, :from => [:committees], :through => :roles end class Role < ActiveRecord::Base belongs_to :user belongs_to :authorizable, :polymorphic => true end class Committee < ActiveRecord::Base has_many :accepted_roles, :as => :authorizable, :class_name => :role, :dependent => :destroy endComments
-
The runtime performance of hmp is killing me in development mode. How can we make this better? Code generation maybe? I'm willing to help build it.
Comments
chriseppstein
Thu Sep 03 17:31:27 -0700 2009
| link
I have this declaration:
acts_as_double_polymorphic_join( :parents => [:pages], :children => [:pages] + Item.type_names(:primary => true), :children_order => "parent_children.position ASC" )And every request does this:
** has_many_polymorphs: associating Page.parents ** has_many_polymorphs: associating Article.parents ** has_many_polymorphs: associating BlogPost.parents ** has_many_polymorphs: associating BuyingGuide.parents ** has_many_polymorphs: associating Calculator.parents ** has_many_polymorphs: associating Checklist.parents ** has_many_polymorphs: associating CommunityPost.parents ** has_many_polymorphs: associating CommunityDiscussion.parents ** has_many_polymorphs: associating Quiz.parents ** has_many_polymorphs: associating MedicareInformation.parents ** has_many_polymorphs: associating News.parents ** has_many_polymorphs: associating Poll.parents ** has_many_polymorphs: associating Question.parents ** has_many_polymorphs: associating Task.parents ** has_many_polymorphs: associating Tip.parents ** has_many_polymorphs: associating ToDoList.parents ** has_many_polymorphs: associating Interview.parents ** has_many_polymorphs: associating Reflection.parents ** has_many_polymorphs: preloading parent model Tag ** has_many_polymorphs: associating Tag.taggablesThere is some env flag that causes it to generate code, which you could paste in...otherwise...cache_classes = true?
chriseppstein
Thu Sep 03 17:35:33 -0700 2009
| link
I tried caching classes. it's not a better solution. The acts_as_double_polymorphic_join declaration rarely changes... it seems silly to do a bunch of work with every request instead of when the declaration changes.
I saw the code generation stuff. I was wondering if we could productize that with some rake tasks, etc.
That seems like it would work...optional way to cache the declarations only, basically.
chriseppstein
Thu Sep 03 17:43:36 -0700 2009
| link
are the "declarations" the hard work that hmp performs?












Is anybody looking into this anyway?!
no. best bet is to make a failing test and fix it yourself; this project is in patch-accepting-mode only :-/
sorry, just wondering if this was the right place to ask these kind of questions. Meanwhile I just stopped trying to put my models into modules: That produces all kinds of nasty side-efffects, not only with has_many_polymorphs - so basically, this works as designed :)
I've got a patch that fixes this (more shortly).
The namespaced_models branch in my fork supports loading names-spaced models in has_many_polymorphs autoload.rb.
Here's the commit where it was added:
http://github.com/stepheneb/has_many_polymorphs/commit/b563f4d65faabc494c8dcef3d3d5c34d2bc2af20