2010-10-30 22 views
1

我不知道如何标题这个问题,它的东西很奇怪。 好了,这里的东西,我创建了一个叫做模型的UserProfiles为其迁移文件看起来像这样:Rails3 ActiveRecords显示错误数据类型的列?

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 

在轨命令提示符[轨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(出生日期)作为1993-07-01存储在MySQL中时,它将其显示为1993.0。我用Navicat验证了存储在MySQL上的数据。所以看起来问题出在一些解码的东西上,我无法确定[作为红宝石和Rails的新手]。 alt text

我的用户配置模式是这样的:

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

任何帮助或见解这个问题是非常赞赏。谢谢!

编辑:

@time机:我尝试使用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 

的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宝石与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。

+0

为什么不使用日期选择器? – 2010-10-30 18:54:00

+0

你可以发布'db/schema.rb'的相关部分吗? – zetetic 2010-10-30 23:36:43

+0

另外,您可以检查1)列类型在数据库控制台中是否正确以及2)您使用的是正确的数据库。 – zetetic 2010-10-30 23:57:23

回答

0

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