Sixty4Bit
Sixty4Bit

Reputation: 13402

Rails: Why is my boolean field returning Fixnum?

We have a boolean field in our system (using Rails 3.0.4) and when attached to MySQL the fields report true and false as appropriate. When attached to Oracle (production) it is returning Fixnum. The column is of type NUMBER(1). Other models correctly return true and false. The controller is doing a simple find. This only happens on the show action. Index returns true false. This happens for both HTML and XML responses. When asking for the type of a field the Console reports a class of FalseClass or TrueClass. The View reports Fixnum.

What could possibly be going wrong?

Updated: Code

create_table "people", :force => true do |t|
  t.string   "name"
  t.boolean  "has_information"
end

class Person < ActiveRecord::Base
  has_many :groups
end

Updated with a clearer question

The Rails convention is to make the field a NUMBER(1) type column. Then converts it to a boolean in the model. This is working in several models in our application. Only ONE model is having this issue. And it only has the issue on the show action. The show action was created by scaffold so is very generic. Why would it Rails not follow convention in this one instance?

Updated with solution

It was fixed in this instance by calling Person.find_by_id!(params[:id]) instead of Person.find(params[:id]) Why the default was returning the wrong class for a field in a normal find is beyond me.

Upvotes: 1

Views: 870

Answers (3)

Sixty4Bit
Sixty4Bit

Reputation: 13402

The answer, update the Oracle Adapter because it is out of date. Version 1.3.0 of the activerecord-oracle_enhanced-adapter gem has a bug that was reported:

https://github.com/rsim/oracle-enhanced/issues/7

We updated the driver and everything works as expected now.

Upvotes: 0

There is no BOOLEAN type in Oracle. We usually use fields of CHAR(1), constrained to have values 'Y' and 'N'. YMMV.

Share and enjoy.

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107728

The checkbox sent back from the view is going to be a number, either "0" or "1" depending on if the checkbox is unchecked or checked respectively. You will need to convert this in your model to a false or true value if that is what your DB adapter requires it to be.

Upvotes: 0

Related Questions