meta_search icon indicating copy to clipboard operation
meta_search copied to clipboard

Polymorphic advanced association raises unexpected error

Open stevenspiel opened this issue 11 years ago • 2 comments

I have a structure set up

class Organization < ActiveRecord::Base
  has_many :groups
end

class Group < ActiveRecord::Base
  has_many :conversations, inverse_of: :group
  belongs_to :organization
end

class Conversation < ActiveRecord::Base
  belongs_to :group, inverse_of: :conversations
  has_many :messages, as: :subject
end

class Message < ActiveRecord::Base
  belongs_to :subject, polymorphic: true
end

When I run the following search, there are no issues. It works great.

Message.search({subject_conversation_type_group_id_eq: 1}) 
# => [<#Message>, <#Message>]

But here's the problem: If I add the next belongs_to association, it fails:

Message.search({subject_conversation_type_group_organization_id_eq: 1}) 
# => ActiveRecord::ConfigurationError: Association named 'group' was not found for 'Message'; perhaps you misspelled it?

Also, when running the search from the level above the polymorphism, it also works fine.

Conversation.search({group_organization_id_eq: 1}) 
# => [<#Conversation>]

Running Rails 3.0.20 and Ruby 1.9.2-p320

stevenspiel avatar Oct 31 '14 17:10 stevenspiel

@stevenspiel Hi, did you got any solution for this problem?.

nishanthan144 avatar Mar 17 '15 13:03 nishanthan144

no... I just had to use Arel to create the query manually.

stevenspiel avatar Apr 21 '15 13:04 stevenspiel