Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

use has_attribute? as per https://github.com/rails/rails/issues/4208 #432

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants

kidpollo commented Apr 2, 2013

The raise_on_missing_attribute seems not to be working. I only tested on rails 3.2.11

It seems the current behavior is respond_to? will always return true so has_attribute? is recommended.

I put this together very quickly. Let me know if further investigation or code is required.

Owner

nesquena commented Apr 2, 2013

I'm not sure this works considering that rabl is agnostic to ORM. I want rabl to work with every ORM and not just activerecord. Why would everything return true as if it responds, that doesn't make sense?

kidpollo commented Apr 2, 2013

I corrected my commit it also needs the respond_to

kidpollo commented Apr 2, 2013

@nesquena you are totally right it does not make sense. You can look at the last comment on rails/rails#4208. In AR the method does exist even if you did not fetch the attribute. @josevalim actually seems to agree the expected behavior should be that it does not respond to the method :@

Without this the raise_on_missing_attribute has no effect on Rails. Could there be an alternative?

kidpollo commented Apr 2, 2013

Never mind my corrected commit does not work either. I fear this requires more complex logic. Back to the drawing board :(

kidpollo commented Apr 2, 2013

The raise_on_missing_attribute option is very useful Rails just does not like it.

kidpollo commented Apr 2, 2013

well AR actually

@kidpollo kidpollo closed this Apr 2, 2013

@kidpollo kidpollo reopened this Apr 2, 2013

kidpollo commented Apr 2, 2013

Re opened @nesquena I came up with a Fugly hack that seems to works for now. I dont expect you will like this but are there other cases where you handle special cases for a ORM? :S

Owner

nesquena commented Apr 2, 2013

It feels too hacky for me to incorporate into rabl but feel free to use it as a fork. I don't know how to fix this but it seems more likely Rails will fix that weird behavior because it shouldn't be returning true for methods it doesn't respond to, thats crazy.

kidpollo commented Apr 2, 2013

I agree

Owner

nesquena commented Apr 2, 2013

Thanks for taking the time to submit a patch though, I appreciate it. Sorry this came up, I'm hoping this is treated as a bug on their end and fixed.

@nesquena nesquena closed this Sep 3, 2013

@kidpollo @nesquena I'm running into this issue as well and don't have a better approach vs forking and patching in your hack to rescue MissingAttributeError. Has any better solution been added?

A more generic solution I had an idea for was if RABL allowed you to rescue exceptions that were thrown when accessing attributes, and then return a value for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment