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

each_instance doesn't handle enum attributes? #40

Closed
chickenburgers opened this issue Aug 18, 2018 · 2 comments
Closed

each_instance doesn't handle enum attributes? #40

chickenburgers opened this issue Aug 18, 2018 · 2 comments

Comments

@chickenburgers
Copy link

First of all, great gem, love it 👍

I've only run into a problem with enum attributes in ActiveRecord. If I define a model with an enum attribute, .each() outputs the symbolic enum value, but .each_instance() outputs the underlying numeric value. e.g.

class User < ApplicationRecord
enum gender: [:male, :female]
end

User.all.each {|u| puts u.gender} # Outputs "male", "male", "female"
User.all.each_instance {|u| puts u.gender} # Outputs 0, 0, 1

@afair
Copy link
Owner

afair commented Sep 27, 2019

Thanks, @chickenburgers (what a cool name!)

I looked on this again today. As far as I can tell, I'm doing the correct thing in instantiating the record. The enum's act as if they aren't defined. Could be an activerecord issue? Or there is a newer way to to it? Frustrating. 😢

@prashantvithani
Copy link
Contributor

@afair I'm wondering why column_types is used in each_instance or each_instance_batch for activerecord version greater than 4 (5.2.3 to be precise). If I instantiate the object with klass.send(:instantiate, row) or simply klass.instantiate(row) (without column_types), it'd map the enum correctly while instantiating the AR object. Am I missing something here?

@afair afair closed this as completed in c6080e1 Jan 17, 2020
afair added a commit that referenced this issue Jan 17, 2020
FIX #40: Handle enum attributes while instantiating rows
afair added a commit that referenced this issue Jan 17, 2020
Revert "FIX #40: Handle enum attributes while instantiating rows"
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