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

a "test" attribute on model will cause an exception #1261

Closed
ryedog opened this issue Oct 9, 2015 · 7 comments
Closed

a "test" attribute on model will cause an exception #1261

ryedog opened this issue Oct 9, 2015 · 7 comments

Comments

@ryedog
Copy link

ryedog commented Oct 9, 2015

If you have an attribute called "test" on your model it will throw an exception

There is a "test" method from the kernel on Serializer so when serializing in the attributes instance method of ActiveModel::Serializer you'll get

 ArgumentError:
       wrong number of arguments (0 for 2..3)
     # /home/vagrant/.rvm/gems/ruby-2.1.1/bundler/gems/active_model_serializers-1f08865a10bb/lib/active_model/serializer.rb:156:in `test'
     # /home/vagrant/.rvm/gems/ruby-2.1.1/bundler/gems/active_model_serializers-1f08865a10bb/lib/active_model/serializer.rb:156:in `block in attributes'

The source of the test method is

ruby-2.1.1-p76 (#<BaseSerializer:0x0000000afe8e40>):0 > show-source self.test

From: file.c (C Method):
Owner: Kernel
Visibility: private
Number of lines: 137

static VALUE
rb_f_test(int argc, VALUE *argv)
{
    int cmd;

    if (argc == 0) rb_check_arity(argc, 2, 3);
    cmd = NUM2CHR(argv[0]);
    if (cmd == 0) {

Digging into the implementation of ActiveModel Serializer it's apparent that any attribute with the same name as a method on the Serializer will have similar issues.

@bf4
Copy link
Member

bf4 commented Oct 9, 2015

yup, sorry about that. do you not have a test method on your resource? I've seen similar issues with select which is an IO mixin that's in Kernel for some reason, and even a BasicObject has it

@bf4
Copy link
Member

bf4 commented Oct 26, 2015

would it be helpful if we just defined a test and select method on the Serializer base object that raises if you try to call it but haven't defined it?

@beauby
Copy link
Contributor

beauby commented Oct 26, 2015

That sounds like a terrible idea, as we would keep defining many such methods as more similar issues arise.

@beauby
Copy link
Contributor

beauby commented Oct 26, 2015

Plus, it does not fix the situation where you have a select attribute on your model, and declare it as attribute :select in your serializer (as the serializer will see that #select is already defined on the serializer, thus not defining it as object.select).

@bf4
Copy link
Member

bf4 commented Oct 26, 2015

Dunno, OP hasn't responded. As to Ruby stdlib gotchas, those are the only two I can think of. Anyhow, I just meant AMS could be nice and somehow tell you that you've asked for the select attribute but it's hitting Kernel. I agree that I'm not sure that would necessarily be the better solution. Hence the question :)

@beauby
Copy link
Contributor

beauby commented Oct 26, 2015

@bf4 There's also binding, open, system among those most likely to cause trouble.

@beauby
Copy link
Contributor

beauby commented Jan 20, 2016

Closing, as a fix has been merged in master. Could you confirm @ryedog ?

@beauby beauby closed this as completed Jan 20, 2016
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