2

I am having trouble accessing an enum value from the view layer in Rails.

I have an ActiveRecord model named Rule which has an enum operation defined:

class Rule < ActiveRecord::Base
  enum operation: [:says, :contains]
end

I have passed a rule to my view and I am trying to access the operation field. The object returns nil, despite having a value.

= rule.inspect             #=> #<Rule id: 2, operation: 1>
= rule.operation           #=> Nil
= rule.operation.class     #=> NilClass

When I attempt to access the operations field from my console, it works just fine.

rule.inspect             #=> #<Rule id: 2, operation: 1>
rule.operation           #=> "contains"

I verified that operation is not a ruby/rails keyword. Is there some trick to getting enums to show up in the view layer?

Thanks in advance!

1
  • I tried renaming the column to 'op' and that did not fix the problem. Commented Jul 7, 2016 at 18:12

1 Answer 1

1

This issue was caused when I clobbered the hash which defined the enum. I did this in order to provide options for a select tag.

@operations = RowRule.operations
# alter @operations here

I solved this by cloning the hash:

@rule_operations = RowRule.operations.clone
# alter @operations here
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.