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

can't get the parent in a polymorphic_belongs_to #158

Closed
wants to merge 2 commits into from
Closed

can't get the parent in a polymorphic_belongs_to #158

wants to merge 2 commits into from

Conversation

indrekj
Copy link
Contributor

@indrekj indrekj commented Aug 23, 2011

I can't access the parent object when I use a polymorphic relation between my controllers.

Following the documentation, I have a controller like this:

class PhotosController < InheritedResources::Base
  belongs_to :user, :task, :polymorphic => true

  def index
    logger.debug(parent.inspect)
  end
end

But parent always returns nil. It doesn't work either with:

polymorphic_belongs_to :user, :task 

However, it works if I don't use a polymorphic relation:

belongs_to :user 

or

belongs_to :task 

Note that my routes and everything else regarding inherited_resources work fine.

I don't know if it is a bug, or if I misinterpreted the documentation.

thanks for your help

julien

@indrekj
Copy link
Contributor

indrekj commented Aug 11, 2011

same problem here

@jujudellago
Copy link
Author

I have created a demo application to demonstrate the problem...

https://github.com/jujudellago/Polymorphic-InheritedResources

@indrekj
Copy link
Contributor

indrekj commented Aug 23, 2011

I think I found the problem.

# Here you can use parent in the views
def show
  show!
end
def show
  resource
  # now you can use parent too
end
def show
  parent # doesn't work, because it is not loaded. @parent_type is nil. It gets loaded after `resource` and after `show!`
end

I added a patch, but its code isn't very nice. Maybe you can get something out of it :).

@jujudellago jujudellago reopened this Aug 25, 2011
@jujudellago
Copy link
Author

I tried to apply the patch and now it works as expected !
Thank you so so so much !!!

(I closed the pull request by mistake, I don't think I should be the one doing that ? anyway the pathe solves my problem...)

@josevalim
Copy link
Contributor

Could someone please rebase this patch? It would be a nice addition.

@indrekj
Copy link
Contributor

indrekj commented Dec 23, 2011

rebased: #189

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

Successfully merging this pull request may close these issues.

4 participants