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

Trying to use acts_as_relation and getting unexpected results. #26

Open
derrelldurrett opened this issue Mar 14, 2013 · 4 comments
Open

Comments

@derrelldurrett
Copy link

I have the following relationships via acts_as_relation:

class Node < ActiveRecord::Base
  acts_as_superclass
  belongs_to :game, polymorphic: true
  #...
end

class Game < ActiveRecord::Base
  acts_as :node
  has_many :ancestors, class_name: 'Node',
           foreign_key: 'ancestor_id'
  #...
end

class Team < ActiveRecord::Base
  acts_as :node
  # ...
end

When I add a Game to the list of ancestors in another Game, I get:

ActiveRecord::AssociationTypeMismatch: Node(#30169720) expected, got Game(#24699180)
/home/rev/.rvm/gems/ruby-1.9.3-p385@global/gems/activerecord-3.2.12/lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global/gems/activerecord-3.2.12/lib/active_record/associations/collection_association.rb:494:in `block in concat_records'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global/gems/activerecord-3.2.12/lib/active_record/associations/collection_association.rb:493:in `each'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global/gems/activerecord-3.2.12/lib/active_record/associations/collection_association.rb:493:in `concat_records'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global/gems/activerecord-3.2.12/lib/active_record/associations/collection_association.rb:132:in `concat'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global/gems/activerecord-3.2.12/lib/active_record/associations/collection_proxy.rb:116:in `<<'
/home/rev/RubymineProjects/AliasMadness/app/models/bracket_factory.rb:17:in `block in create_bracket'

If I examine the Game object in the debugger, object.is_a? Node returns false, which makes no sense, given the above.

Do you have any suggestions for how I can proceed? This is rails 3.2.12 under ruby 1.9.3.

@hzamani
Copy link
Owner

hzamani commented Mar 14, 2013

A game is a node an belongs to another node? Even if this is the case,
polymorphic makes no sense. We usually don't use a model name as a
polymorphic relation name.

This was what I got on the first look. I am going on a trip with no
internet access, so next reply will be on next week!
On Mar 14, 2013 3:42 AM, "derrelldurrett" notifications@github.com wrote:

I have the following relationships via acts_as_relation:

class Node < ActiveRecord::Base
acts_as_superclass
belongs_to :game, polymorphic: true
#...end
class Game < ActiveRecord::Base
acts_as :node
has_many :ancestors, class_name: 'Node',
foreign_key: 'ancestor_id'
#...end
class Team < ActiveRecord::Base
acts_as :node

...end

When I add a to the list of in another , I get:

ActiveRecord::AssociationTypeMismatch: Node(#30169720) expected, got
Game(#24699180)
/home/rev/.rvm/gems/ruby-1.9.3-p385@global/gems/activerecord-3.2.12/lib/active_record/associations/association.rb:204:in
raise_on_type_mismatch'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global
/gems/activerecord-3.2.12/lib/active_record/associations/collection_association.rb:494:inblock
in concat_records'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global/gems/activerecord-3.2.12/lib/active_record/associations/collection_association.rb:493:in
each'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global
/gems/activerecord-3.2.12/lib/active_record/associations/collection_association.rb:493:in
concat_records'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global/gems/activerecord-3.2.12/lib/active_record/associations/collection_association.rb:132:in
concat'
/home/rev/.rvm/gems/ruby-1.9.3-p385@global
/gems/activerecord-3.2.12/lib/active_record/associations/collection_proxy.rb:116:in
<<'
/home/rev/RubymineProjects/AliasMadness/app/models/bracket_factory.rb:17:in
`block in create_bracket'

If I examine the Game object in the debugger, returns , which makes no
sense, given the above.

Do you have any suggestions for how I can proceed? This is rails 3.2.12
under ruby 1.9.3.


Reply to this email directly or view it on GitHubhttps://github.com//issues/26
.

@derrelldurrett
Copy link
Author

Nodes 1

So, yes, Games belong to and are Nodes. Teams are also Nodes. Games 1) must have two "ancestors", which can be either a Game or a Team, 2) eventually have another Team (which relationship is not shown for purposes of clarity).

Part of your comment doesn't make much sense to me (which suggests I'm doing something wrong): "We don't usually use a model name as a polymorphic relation name." I went through several iterations of code attempting to get a working relationship, and what you see above was the version that managed to get through the compile step.

Previous attempts included this combination:

class Node < ActiveRecord::Base
  acts_as_superclass
  belongs_to :ancestor, polymorphic: true
  # ...
end

class Game < ActiveRecord::Base
  acts_as :node
  has_many :nodes, as: :ancestor
  # ...
end

Those would fail because ancestors wasn't a method on a Game object.

@hzamani
Copy link
Owner

hzamani commented Jul 15, 2013

Maybe it's to late! But why polymorphic: true is there?

@derrelldurrett
Copy link
Author

Not too late (I'm always looking to understand what I missed the first time through).

In the earlier attempt, I was trying to express the relationship that Nodes, being the superclass, were potentially either Games or Teams.

I've been away from this for some time. So, if I'm saying something incomprehensible, it may be a lack of memory or a genuine lack of understanding something. If you could be more verbose and explicit, that would be helpful.

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

2 participants