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

association with a class_name parameter #7

Open
keien opened this issue Mar 9, 2013 · 2 comments
Open

association with a class_name parameter #7

keien opened this issue Mar 9, 2013 · 2 comments

Comments

@keien
Copy link

keien commented Mar 9, 2013

Hi,

First of all, great gem! This is exactly what I needed for my Rails app.

Unfortunately, I had an issue with associations that have a different name than their class, for reversing relationships with two foreign keys from the same model. Here's what it looks like:

has_many :relationships, foreign_key: "subject_id", dependent: :delete_all
has_many :reverse_relationships, class_name: "Relationship",
                                             foreign_key: "target_id",
                                             dependent: :delete_all

I think it's a problem with the way you access the model class. As far as I know, this is what the gem does:

sub_model_class = :association_name.to_s.classify.constantize

Since ReverseRelationship is not a class, this gives me this error when using recuse_delete: NameError: uninitialized constant ReverseRelationship

It'd be great if you could have this working for associations that use class_name as well.

Thanks!

@jisaacks
Copy link
Owner

jisaacks commented Mar 9, 2013

There is a part in the gem that looks at the association and gets the class name like this:

assoc.name.to_s.classify.constantize

You should look at the assoc object and see if there is a class_name property. If so, it could be fixed by doing something like this:

(assoc.class_name || assoc.name).to_s.classify.constantize

I don't have time to mess with it right now myself.

@ujh
Copy link

ujh commented Jun 7, 2013

I think you can use assoc.klass for this (yes that's not a typo).

jisaacks added a commit that referenced this issue Jan 29, 2015
fix for associations with class_name parameter, issue #7
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

3 participants