Note that there are some explanatory texts on larger screens.

plurals
  1. PORails3 ActiveRecords displaying column with wrong datatype?
    primarykey
    data
    text
    <p>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:</p> <pre><code> class CreateUserProfiles &lt; ActiveRecord::Migration def self.up create_table :user_profiles, :primary_key =&gt; :id, :options =&gt; "auto_increment = 1" do |t| t.integer :id t.references :user t.string :first_name, :limit =&gt; 20 t.string :middle_name, :limit =&gt; 20 t.string :last_name, :limit =&gt; 20 t.string :address_line_1, :limit =&gt; 50 t.string :address_line_2, :limit =&gt; 50 t.string :address_line_3, :limit =&gt; 50 t.string :city, :limit =&gt; 20 t.string :state, :limit =&gt; 20 t.string :country, :limit =&gt; 2 t.string :zipcode, :limit =&gt; 10 t.string :phone, :limit =&gt; 20 t.string :mobile, :limit =&gt; 20 t.string :email, :limit =&gt; 100 t.string :photo_url, :limit =&gt; 256 t.integer :photo_id t.date :dob t.string :passport_no, :limit =&gt; 20 t.string :nationality, :limit =&gt; 2 #t.string :ssr_meal, :limit =&gt; 20 # t.string :ssr_disability, :limit =&gt; 20 # t.string :ssr_seating, :limit =&gt; 20 # t.string :ssr_other, :limit =&gt; 20 t.timestamps end end def self.down drop_table :user_profiles end end </code></pre> <p>in the rails command prompt [rails c], when I type: "UserProfiles", I get the below:</p> <pre><code>irb(main):009:0&gt; UserProfile =&gt; 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) </code></pre> <p>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:</p> <pre><code>irb(main):010:0&gt; UserProfile.all =&gt; [#&lt;UserProfile id: 19, user_id: 1, first_name: "Sriram", middle_name: "", last_name: "C handrasekaran", address_line_1: "", address_line_2: "", address_line_3: #&lt;BigDecimal:4f4f3 28,'0.0',4(8)&gt;, city: "", state: #&lt;BigDecimal:4f4f280,'0.0',4(8)&gt;, country: nil, zipcode: 0.0, phone: 0, mobile: 0, email: nil, photo_url: nil, photo_id: nil, dob: #&lt;BigDecimal:4f4 f0b8,'0.1993E4',4(8)&gt;, passport_no: #&lt;BigDecimal:4f4f058,'0.0',4(8)&gt;, nationality: "IN", c reated_at: "2010-10-30 17:32:11", updated_at: "2010-10-30 18:35:36"&gt;] irb(main):011:0&gt; </code></pre> <p>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].<img src="https://i.stack.imgur.com/4njDS.jpg" alt="alt text"></p> <p>My UserProfile model looks like this:</p> <pre><code>class UserProfile &lt; ActiveRecord::Base belongs_to :user has_one :primary_user_profile end </code></pre> <p>Any help or insights into this problem is much appreciated. Thanks!</p> <p><strong>Edit:</strong></p> <p>@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.</p> <p>@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</p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20101026142802) do # Could not dump table "user_profiles" because of following ArgumentError # invalid date end </code></pre> <p>The 2 commented lines seem to repeat themselves over for every table in the database/ every model migrated through ruby.</p> <p>From my database console:</p> <pre><code>mysql&gt; 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) </code></pre> <p>When I look through navicat, the data from the form is successfully stored in the database, but displaying it seems to be the problem.</p> <p>EDIT 2:</p> <p>The issue seems to be with the way the mysql2 gem interacts with MySQL. I've switched over to PostgreSQL and the issue vanished.</p> <p>Schema.rb now reads:</p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20101026142802) do create_table "flight_prices", :force =&gt; true do |t| t.integer "flight_schedule_id" t.decimal "price_usd", :precision =&gt; 8, :scale =&gt; 2 t.decimal "price_other", :precision =&gt; 10, :scale =&gt; 2 t.string "price_other_country", :limit =&gt; 2 t.datetime "created_at" t.datetime "updated_at" t.string "booking_code", :limit =&gt; 2 t.string "booking_class", :limit =&gt; 20 t.string "fare_code", :limit =&gt; 10 t.text "fare_rules" end create_table "flight_schedules", :force =&gt; true do |t| t.string "id_iata", :limit =&gt; 3 t.string "id_icao", :limit =&gt; 3 t.string "flight_number", :limit =&gt; 7 t.string "departure_airport", :limit =&gt; 3 t.string "arrival_airport", :limit =&gt; 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 =&gt; 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 =&gt; true do |t| t.string "iata", :limit =&gt; 3 t.string "icao", :limit =&gt; 3 t.string "name", :limit =&gt; 64 t.string "country_iso", :limit =&gt; 2 end create_table "static_airports", :force =&gt; true do |t| t.string "iata", :limit =&gt; 3 t.string "label", :limit =&gt; 128 t.string "name", :limit =&gt; 128 t.string "locale", :limit =&gt; 128 t.string "static_country_iso", :limit =&gt; 2 t.datetime "created_at" t.datetime "updated_at" end create_table "static_countries", :force =&gt; true do |t| t.string "iso", :limit =&gt; 2 t.string "label_english", :limit =&gt; 64 t.string "label_local", :limit =&gt; 64 end create_table "user_profiles", :force =&gt; true do |t| t.integer "user_id" t.string "first_name", :limit =&gt; 20 t.string "middle_name", :limit =&gt; 20 t.string "last_name", :limit =&gt; 20 t.string "address_line_1", :limit =&gt; 50 t.string "address_line_2", :limit =&gt; 50 t.string "address_line_3", :limit =&gt; 50 t.string "city", :limit =&gt; 20 t.string "state", :limit =&gt; 20 t.string "country", :limit =&gt; 2 t.string "zipcode", :limit =&gt; 10 t.string "phone", :limit =&gt; 20 t.string "mobile", :limit =&gt; 20 t.string "email", :limit =&gt; 100 t.string "photo_url", :limit =&gt; 256 t.integer "photo_id" t.date "dob" t.string "passport_no", :limit =&gt; 20 t.string "nationality", :limit =&gt; 2 t.datetime "created_at" t.datetime "updated_at" end create_table "users", :force =&gt; true do |t| t.string "email", :default =&gt; "", :null =&gt; false t.string "encrypted_password", :limit =&gt; 128, :default =&gt; "", :null =&gt; false t.string "password_salt", :default =&gt; "", :null =&gt; false t.string "reset_password_token" t.string "remember_token" t.datetime "remember_created_at" t.integer "sign_in_count", :default =&gt; 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 =&gt; "index_users_on_email", :unique =&gt; true add_index "users", ["reset_password_token"], :name =&gt; "index_users_on_reset_password_token", :unique =&gt; true end </code></pre> <p>Being a mere mortal in the domain of Ruby &amp; Rails, im unable to fix the mysql2 bug. it is listed here <a href="http://github.com/brianmario/mysql2/issues#issue/71" rel="nofollow noreferrer">GitHub</a> by someone else.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload