开发者

Rails3 ActiveRecords displaying column with wrong datatype?

I am not sure how to title this question, its something rather weird. Well, here is the thing, I've created a model called UserProfiles for which the migration file looks like this:

  class CreateUserProfiles < ActiveRecord::Migration
  def self.up
    create_table :user_profiles, :primary_key => :id, :options => "auto_increment = 1"  do |t|
      t.integer :id
      t.references :user
      t.string  :first_name, :limit   => 20
      t.string  :middle_name, :limit   => 20
      t.string  :last_name, :limit   => 20

      t.string  :address_line_1, :limit   => 50
      t.string  :address_line_2, :limit   => 50
      t.string  :address_line_3, :limit   => 50
      t.string  :city, :limit   => 20
      t.string  :state, :limit   => 20
      t.string  :country, :limit   => 2
      t.string  :zipcode, :limit  => 10

      t.string  :phone, :limit   => 20
      t.string  :mobile, :limit   => 20
      t.string  :email,  :limit   => 100


      t.string  :photo_url, :limit  => 256
      t.integer :photo_id

      t.date    :dob
      t.string  :passport_no, :limit => 20
      t.string  :nationality, :limit => 2


      #t.string  :ssr_meal,   :limit => 20
     # t.string  :ssr_disability, :limit  => 20
     # t.string  :ssr_seating, :limit  => 20
     # t.string  :ssr_other, :limit  => 20


      t.timestamps
    end
  end

  def self.down
    drop_table :user_profiles
  end
end

in the rails command prompt [rails c], when I type: "UserProfiles", I get the below:

irb(main):009:0> UserProfile
=> UserProfile(id: integer, user_id: integer, first_name: string, middle_name: string, las
t_name: string, address_line_1: string, address_line_2: string, address_line_3: string, ci
ty: string, state: string, country: string, zipcode: string, phone: string, mobile: string
, email: string, photo_url: string, photo_id: integer, dob: date, passport_no: string, nat
ionality: string, created_at: datetime, updated_at: datetime)

The problem is, when i output/ get a particular record via UserProfiles.all or UserProfiles.find, It seems like ActiveRecords is jumbling up the datatypes and is showing me invalid data, as in the case below:

irb(main):010:0> UserProfile.all
=> [#<UserProfile id: 19, user_id: 1, first_name: "Sriram", middle_name: "", last_name: "C
handrasekaran", address_line_1: "", address_line_2: "", address_line_3: #<BigDecimal:4f4f3
28,'0.0',4(8)>, city: "", state: #<BigDecimal:4f4f280,'0.0',4(8)>, country: nil, zipcode:
0.0, phone: 0, mobile: 0, email: nil, photo_url: nil, photo_id: nil, dob: #<BigDecimal:4f4
f0b8,'0.1993E4',4(8)>, passport_no: #<BigDecimal:4f4f058,'0.0',4(8)>, nationality: "IN", c
reated_at: "2010-10-30 17:32:11", updated_at: "2010-10-30 18:35:36">]
irb(main):011:0>

So while the DOB (Date of birth) is stored in MySQL as 1993-07-01 , it displays it as 1993.0. I have verified the data stored on MySQL with Navicat. So it would seem the problem is with some decoding thing, which I am unable to identify [being a novice in ruby and rails].

Rails3 ActiveRecords displaying column with wrong datatype?

My UserProfile model looks like this:

class UserProfile < Ac开发者_开发百科tiveRecord::Base
  belongs_to  :user
  has_one :primary_user_profile
end

Any help or insights into this problem is much appreciated. Thanks!

Edit:

@Time Machine: I tried using the date_select form helper but it would populate only the year. As in. i could use it push the data to the database..but when it came to displaying it, only the year would show. Just like the textbox.

@Zetac: My schema.rb file has lots of commented errors.. not sure what to make of it. All the tables were designed through migrations only. Schema.rb

ActiveRecord::Schema.define(:version => 20101026142802) do
# Could not dump table "user_profiles" because of following ArgumentError
#   invalid date
end

The 2 commented lines seem to repeat themselves over for every table in the database/ every model migrated through ruby.

From my database console:

mysql> desc travel.user_profiles;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id        | int(11)      | YES  |     | NULL    |                |
| first_name     | varchar(20)  | YES  |     | NULL    |                |
| middle_name    | varchar(20)  | YES  |     | NULL    |                |
| last_name      | varchar(20)  | YES  |     | NULL    |                |
| address_line_1 | varchar(50)  | YES  |     | NULL    |                |
| address_line_2 | varchar(50)  | YES  |     | NULL    |                |
| address_line_3 | varchar(50)  | YES  |     | NULL    |                |
| city           | varchar(20)  | YES  |     | NULL    |                |
| state          | varchar(20)  | YES  |     | NULL    |                |
| country        | varchar(3)   | YES  |     | NULL    |                |
| zipcode        | varchar(10)  | YES  |     | NULL    |                |
| phone          | varchar(20)  | YES  |     | NULL    |                |
| mobile         | varchar(20)  | YES  |     | NULL    |                |
| email          | varchar(100) | YES  |     | NULL    |                |
| photo_url      | varchar(256) | YES  |     | NULL    |                |
| photo_id       | int(11)      | YES  |     | NULL    |                |
| dob            | date         | YES  |     | NULL    |                |
| passport_no    | varchar(20)  | YES  |     | NULL    |                |
| nationality    | varchar(3)   | YES  |     | NULL    |                |
| created_at     | datetime     | YES  |     | NULL    |                |
| updated_at     | datetime     | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
22 rows in set (0.01 sec)

When I look through navicat, the data from the form is successfully stored in the database, but displaying it seems to be the problem.

EDIT 2:

The issue seems to be with the way the mysql2 gem interacts with MySQL. I've switched over to PostgreSQL and the issue vanished.

Schema.rb now reads:

ActiveRecord::Schema.define(:version => 20101026142802) do

  create_table "flight_prices", :force => true do |t|
    t.integer  "flight_schedule_id"
    t.decimal  "price_usd",                         :precision => 8,  :scale => 2
    t.decimal  "price_other",                       :precision => 10, :scale => 2
    t.string   "price_other_country", :limit => 2
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "booking_code",        :limit => 2
    t.string   "booking_class",       :limit => 20
    t.string   "fare_code",           :limit => 10
    t.text     "fare_rules"
  end

  create_table "flight_schedules", :force => true do |t|
    t.string   "id_iata",           :limit => 3
    t.string   "id_icao",           :limit => 3
    t.string   "flight_number",     :limit => 7
    t.string   "departure_airport", :limit => 3
    t.string   "arrival_airport",   :limit => 3
    t.datetime "departure"
    t.datetime "departure_utc"
    t.datetime "arrival"
    t.datetime "arrival_utc"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "primary_user_profiles", :force => true do |t|
    t.integer  "user_id"
    t.integer  "user_profile_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "static_airlines", :force => true do |t|
    t.string "iata",        :limit => 3
    t.string "icao",        :limit => 3
    t.string "name",        :limit => 64
    t.string "country_iso", :limit => 2
  end

  create_table "static_airports", :force => true do |t|
    t.string   "iata",               :limit => 3
    t.string   "label",              :limit => 128
    t.string   "name",               :limit => 128
    t.string   "locale",             :limit => 128
    t.string   "static_country_iso", :limit => 2
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "static_countries", :force => true do |t|
    t.string "iso",           :limit => 2
    t.string "label_english", :limit => 64
    t.string "label_local",   :limit => 64
  end

  create_table "user_profiles", :force => true do |t|
    t.integer  "user_id"
    t.string   "first_name",     :limit => 20
    t.string   "middle_name",    :limit => 20
    t.string   "last_name",      :limit => 20
    t.string   "address_line_1", :limit => 50
    t.string   "address_line_2", :limit => 50
    t.string   "address_line_3", :limit => 50
    t.string   "city",           :limit => 20
    t.string   "state",          :limit => 20
    t.string   "country",        :limit => 2
    t.string   "zipcode",        :limit => 10
    t.string   "phone",          :limit => 20
    t.string   "mobile",         :limit => 20
    t.string   "email",          :limit => 100
    t.string   "photo_url",      :limit => 256
    t.integer  "photo_id"
    t.date     "dob"
    t.string   "passport_no",    :limit => 20
    t.string   "nationality",    :limit => 2
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "email",                               :default => "", :null => false
    t.string   "encrypted_password",   :limit => 128, :default => "", :null => false
    t.string   "password_salt",                       :default => "", :null => false
    t.string   "reset_password_token"
    t.string   "remember_token"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                       :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

end

Being a mere mortal in the domain of Ruby & Rails, im unable to fix the mysql2 bug. it is listed here GitHub by someone else.

Also for background information, if anyone else can fix the issue. Im running Rails 3.0.1, MySQL 5.1 32bit on Windows Server 2008R2 (64bit), Ruby version 1.9.2 and the latest mysql2 gem.


Change the dob datatype to datetime instead of date.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜