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

ActiveRecord::InverseOfAssociationNotFoundError with self join #975

Closed
vala opened this issue Feb 10, 2012 · 2 comments
Closed

ActiveRecord::InverseOfAssociationNotFoundError with self join #975

vala opened this issue Feb 10, 2012 · 2 comments

Comments

@vala
Copy link

vala commented Feb 10, 2012

Hi,

I've been stuck with a ActiveRecord::InverseOfAssociationNotFoundError while trying to make a self join in some model.

The model relevant code :

class Category < ActiveRecord::Base
  belongs_to :parent_category, :class_name => 'Category', :foreign_key => 'parent_id', :inverse_of => 'children_categories'
  has_many :children_categories, :class_name => 'Category', :foreign_key => 'parent_id', :inverse_of => 'parent_category'
end

Rails recognize my association and everything is working fine.
If I avoid :inverse_of param I can get to make RA work but the parent_category field is a text_field instead of the desired dropdown and children_categories is not shown.
The field children_categories appears if I force it from the rails_admin.rb initializer but shows me a dropdown select instead of multiple select.

EDIT : Like my previous issue, I'm on Rails 3.2 ... if it matters

I don't know if it's a known issue or if I'm making something wrong, but I'll try to figure it out.

@vala vala closed this as completed Feb 10, 2012
@vala
Copy link
Author

vala commented Feb 10, 2012

Well, avoiding the :inverse_of finally worked. I admit I can't tell why it didn't worked before since I don't remember doing any action between the time it failed and now that it works apart from modifying the way I declare my relations.

So the working self-join relation :

class Category < ActiveRecord::Base
  belongs_to :parent_category, :class_name => 'Category', :foreign_key => 'parent_id'
  has_many :children_categories, :class_name => 'Category', :foreign_key => 'parent_id'
end

I think I'm misunderstanding something, but well, if it can be helpful.

Have a good day

@narokir
Copy link

narokir commented Aug 3, 2012

There are a few limitations to inverse_of support:

They do not work with :through associations.
They do not work with :polymorphic associations.
They do not work with :as associations.
For belongs_to associations, has_many inverse associations are ignored.

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