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
Reader / predicate returning different values for boolean field #17415
Comments
This method should be so: test 'predicate method' do
assert_equal false, @klass.setting?
@klass.setting = 1
assert_equal true, @klass.setting?
end or test 'predicate method' do
@klass.setting = 0
assert_equal false, @klass.setting?
@klass.setting = 1
assert_equal true, @klass.setting?
end On first way is |
This is the expected behavior. The predicate checks for emptiness, not truthiness. Please see the documentation. http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods.html#method-i-attribute_present-3F |
Yep @sgrif, but others fields have other behaviors [21] pry(main)> TaskItem.find(241).ancestor
TaskItem Load (1.1ms) SELECT "task_items".* FROM "task_items" WHERE "task_items"."id" = $1 LIMIT 1 [["id", 241]]
=> false
[22] pry(main)> TaskItem.find(241).ancestor?
TaskItem Load (0.8ms) SELECT "task_items".* FROM "task_items" WHERE "task_items"."id" = $1 LIMIT 1 [["id", 241]]
=> false
|
Your pry output is making it difficult to understand your problem. Can you please provide an executable test case using this as a template? https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_master.rb |
Something very weird is going on. The expected behaviour is to return |
Whoops, you are correct I got the suffix mixed up. |
Everything is working fine, here is the gist. Ruby version: ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin13.0] |
@kuldeepaggarwal This problem happened in one field, the reason I don't know. Other boolean fields works!! |
This could be happening because you may have an enum that defines a value with the same name as the boolean field in your table. Since enum values results in predicate methods, you're getting the false value because your object doesn't have that enum "type" assigned to it. The question is: In case we have a field with the same name as a enum value, what should be the correct behavior ? Ask the database field or the enum ? |
@guilhermeFranco is right. I miss the enum declaration with the same name as the boolean field. BTW, shouldn't the FW inform us? |
@thiagogsr it does it but for methods already defined by the framework. User defined methods don't give any warning. |
@rafaelfranca I asked because the FW raises a fatal error when I try to use a enum item with same name of any relationship example: class User < ActiveRecord::Base
enum role: [ :admin, :parent, :manager ]
belongs_to :parent
end |
@thiagogsr You have a boolean field as well for counselor, so rails will create 2 methods |
I think Rails behaviour is correct in this case. |
@kuldeepaggarwal yep, I just suggested an 'stack level too deep' on this case |
How about prefixing enum generated methods like |
@prathamesh-sonpatki I would prefer a combination of the enum name and its value. Given the following model: class User < ActiveRecord::Base
enum role: [ :admin, :parent, :manager ]
belongs_to :parent
end The enum value verification could be:
|
Awesome. This makes sense 😄 |
Something like this? prathamesh-sonpatki@c314864 |
I think then all methods should be prefixed when created by enum. |
I think we should raise error or give warning in this scenario. |
This issue has been automatically marked as stale because it has not been commented on for at least The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the Thank you for all your contributions. |
We can close this issue as per the discussion in #17506 |
Closing this since #19813 has already been merged. |
Is it a bug?
The text was updated successfully, but these errors were encountered: