【问题标题】:Rails3 ActiveRecords displaying column with wrong datatype?Rails3 ActiveRecords 显示列的数据类型错误?
【发布时间】:2010-10-30 18:48:06
【问题描述】:

我不知道如何命名这个问题,它有点奇怪。 好吧,事情就是这样,我创建了一个名为 UserProfiles 的模型,其迁移文件如下所示:

  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

在 rails 命令提示符 [rails c] 中,当我输入:“UserProfiles”时,我得到以下信息:

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)

问题是,当我通过 UserProfiles.all 或 UserProfiles.find 输出/获取特定记录时,ActiveRecords 似乎正在混淆数据类型并向我显示无效数据,如下所示:

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>

因此,虽然 DOB(出生日期)在 MySQL 中存储为 1993-07-01 ,但它显示为 1993.0。我已经用 Navicat 验证了存储在 MySQL 上的数据。所以看起来问题出在一些解码方面,我无法识别[作为 ruby​​ 和 rails 的新手]。

我的 UserProfile 模型如下所示:

class UserProfile < ActiveRecord::Base
  belongs_to  :user
  has_one :primary_user_profile
end

非常感谢您对此问题的任何帮助或见解。谢谢!

编辑:

@Time Machine:我尝试使用 date_select 表单助手,但它只会填充年份。如。我可以使用它将数据推送到数据库..但是在显示它时,只会显示年份。就像文本框一样。

@Zetac:我的 schema.rb 文件有很多注释错误。不知道该怎么做。所有表都是仅通过迁移设计的。 Schema.rb

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

对于数据库中的每个表/通过 ruby​​ 迁移的每个模型,这 2 行注释似乎都在重复。

从我的数据库控制台:

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)

当我通过navicat查看时,表单中的数据已成功存储在数据库中,但显示它似乎是个问题。

编辑 2:

问题似乎在于 mysql2 gem 与 MySQL 交互的方式。我已经切换到 PostgreSQL,问题就消失了。

Schema.rb 现在变为:

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

作为 Ruby & Rails 领域的普通人,我无法修复 mysql2 错误。它被其他人列在这里GitHub

还提供背景信息,如果其他人可以解决此问题。我在 Windows Server 2008R2(64 位)上运行 Rails 3.0.1、MySQL 5.1 32 位、Ruby 版本 1.9.2 和最新的 mysql2 gem。

【问题讨论】:

  • 可以发db/schema.rb的相关部分吗?
  • 另外,您能否检查一下 1) 列类型在数据库控制台中看起来是否正确,以及 2) 您使用的是正确的数据库。
  • 在“编辑”部分添加了信息。谢谢:)

标签: mysql ruby-on-rails activerecord ruby-on-rails-3


【解决方案1】:

将 dob 数据类型更改为 datetime 而不是 date。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多