[Rails] why would an int column in a join table be a String class in model?

Chris Hall christopher.k.hall at gmail.com
Wed Dec 14 15:11:30 GMT 2005


i've got an issue with integer columns in the database becoming strings in
the model and I'm not sure why this is happening.  perhaps someone can
explain.

given the following setup (simplified for example)

tables:

users
----------
id - int
name - varchar(60)

roles
----------
id - int
name - varchar(60)

roles_users (join table)
----------
role_id - int
user_id - int
default_flag - int

data:

user => 1, 'joe'
role => 1, 'admin'
roles_users => 1, 1, 1

models:

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles
end

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users
end

console:

>> user = User.find(1)
=> #<User:0xb771b494 @attributes={"id"=>"1", "location_id"=>nil,
"active_flag"=>"1", "name"=>"joe"}
>>> user.roles[0].default_flag.class
=> String
>> user.roles[0].id.class
=> Fixnum


shouldn't default_flag be a Fixnum?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20051214/b5d9bd33/attachment.html


More information about the Rails mailing list