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

Bug in accessing object in child block #393

Open
nirvdrum opened this issue Jan 21, 2013 · 11 comments
Open

Bug in accessing object in child block #393

nirvdrum opened this issue Jan 21, 2013 · 11 comments

Comments

@nirvdrum
Copy link

According to the docs it's possible to pass the current object to a child block like so:

object @user
child :posts do |user|
  attribute :title unless user.suspended?
end

What I'm seeing is that the block argument is not the "user" but rather the "posts" ActiveRecord relation. I'm not sure if this a bug in the docs or one in the implementation. If the former, how does one access the user object when processing its posts?

@tobowers
Copy link

+1 we're seeing this in the latest version

@mendelk
Copy link

mendelk commented Feb 4, 2014

👍

+100. Is no one else getting this error?

Seems pretty big to me.

@andre1810
Copy link

+1

@nesquena
Copy link
Owner

nesquena commented Feb 1, 2015

Can anyone take a look at the "why" this is happening? It does seem like a pretty big regression if this is happening. Can someone play around with older versions and identify when this regression happened? Maybe @DouweM might be able to take a look eventually too. Thanks.

@DouweM
Copy link
Collaborator

DouweM commented Feb 1, 2015

I'm almost sure this has always been the behavior; the documentation has just been incorrect. In the commit where this feature was introduced, a895fcc, you can see it's passing @_data to the block, which is the child-object at this point. The spec may be a little misleading, but here too, the passed object is @user-the-argument-to-child, not @user-the-root-object.

@nesquena
Copy link
Owner

nesquena commented Feb 1, 2015

I see, thanks for investigating that. Looks like the docs are at fault leading to the confusion and the years since I implemented that have made the details of how that worked hazy.

@andre1810
Copy link

Can you or anyone implement this? I think many users are waiting for this feature.. Would be great :-)

@DouweM
Copy link
Collaborator

DouweM commented Feb 1, 2015

Note that the original pull request (#300) explicitly calls for the current behavior, not the documented behavior, and it seems unwise to introduce such a backward-incompatible change seeing as it's worked this way for 3 years.

We could add a second argument to be passed to the block which would be the root object "one level up" rather than the root object to the child call:

object @user
child :posts do |posts, user|
  attribute :title unless user.suspended?
end

@andre1810
Copy link

I agree that changing the current behavior would lead to confusion.

Adding a second argument to access the correct object in my collection would solve my problem.

@gsobrevilla
Copy link

+1

@drewB
Copy link

drewB commented Feb 15, 2016

anyone know a work around for this so you can access the parent?

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

8 participants