我不确定如何标题这个问题,它的内容相当奇怪。 嗯,这就是问题,我创建了一个名为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(出生日期)在1993-07-01中存储在MySQL中,但它显示为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&amp; amp; amp; amp; Rails,我无法修复mysql2错误。它由其他人列在GitHub。
另外,对于背景信息,如果其他人可以解决问题。我在Windows Server 2008R2(64位),Ruby版本1.9.2和最新的mysql2 gem上运行Rails 3.0.1,MySQL 5.1 32位。
答案 0 :(得分:0)
将dob数据类型更改为datetime而不是date。