簡體   English   中英

Postgresql表值與Ruby on Rails Active Record查詢結果不匹配

[英]Postgresql table values don't match Ruby on Rails Active Record query results

我在Postgresql中有一個名為monthly_trips的表。 有一行值為id = 3&al = 107.415。 我的問題是當我嘗試訪問rails中“al”列的值時。 使用Rails控制台和下面的代碼我得到的結果是6154.426156397738。 我不知道是什么產生了這個更大的數字(確切地說是57.295779513倍)。 任何幫助將不勝感激! 如果您需要更多信息,請與我們聯系。

trip = MonthlyTrip.find(3)
result = trip.al

monthly_trip.rb

class MonthlyTrip < ActiveRecord::Base
  attr_accessible :al, :month, :vehicle_id, :year
  belongs_to :vehicle
end

psql輸出(選擇id,來自monthly_trips,其中id = 3;)

 id |   al    
----+---------
  3 | 107.415
(1 row)

psql輸出(\\ d monthly_trips)

   Table "public.monthly_trips"
   Column   |            Type             |                         Modifiers                          
------------+-----------------------------+------------------------------------------------------------
 id         | integer                     | not null default nextval('monthly_trips_id_seq'::regclass)
 vehicle_id | integer                     | 
 year       | integer                     | 
 month      | integer                     | 
 al         | numeric(9,3)                | 
 ak         | numeric(9,3)                | 
 az         | numeric(9,3)                | 
 ar         | numeric(9,3)                | 
 ca         | numeric(9,3)                | 
 co         | numeric(9,3)                | 
 ct         | numeric(9,3)                | 
 de         | numeric(9,3)                | 
 fl         | numeric(9,3)                | 
 ga         | numeric(9,3)                | 
 hi         | numeric(9,3)                | 
 ida        | numeric(9,3)                | 
 il         | numeric(9,3)                | 
 ind        | numeric(9,3)                | 
 ia         | numeric(9,3)                | 
 ks         | numeric(9,3)                | 
 ky         | numeric(9,3)                | 
 la         | numeric(9,3)                | 
 me         | numeric(9,3)                | 
 md         | numeric(9,3)                | 
 ma         | numeric(9,3)                | 
 mi         | numeric(9,3)                | 
 mn         | numeric(9,3)                | 
 ms         | numeric(9,3)                | 
 mo         | numeric(9,3)                | 
 mt         | numeric(9,3)                | 
 ne         | numeric(9,3)                | 
 nv         | numeric(9,3)                | 
 nh         | numeric(9,3)                | 
 nj         | numeric(9,3)                | 
 nm         | numeric(9,3)                | 
 ny         | numeric(9,3)                | 
 nc         | numeric(9,3)                | 
 nd         | numeric(9,3)                | 
 oh         | numeric(9,3)                | 
 ok         | numeric(9,3)                | 
 ore        | numeric(9,3)                | 
 pa         | numeric(9,3)                | 
 ri         | numeric(9,3)                | 
 sc         | numeric(9,3)                | 
 sd         | numeric(9,3)                | 
 tn         | numeric(9,3)                | 
 tx         | numeric(9,3)                | 
 ut         | numeric(9,3)                | 
 vt         | numeric(9,3)                | 
 va         | numeric(9,3)                | 
 wa         | numeric(9,3)                | 
 wv         | numeric(9,3)                | 
 wi         | numeric(9,3)                | 
 wy         | numeric(9,3)                | 
 created_at | timestamp without time zone | not null
 updated_at | timestamp without time zone | not null
Indexes:
    "monthly_trips_pkey" PRIMARY KEY, btree (id)

遷移文件

class CreateMonthlyTrips < ActiveRecord::Migration
  def change
    create_table :monthly_trips do |t|
      t.integer :vehicle_id
      t.integer :year
      t.integer :month
      t.decimal :al, precision: 9, scale: 3
      t.timestamps
    end
  end
end

pg_locks表

locktype | database | relation | virtualxid | virtualtransaction | pid | mode | granted

"relation" | 11955 | 11000 | blank | "8/18417" | 25475 | "AccessShareLock" | TRUE

"virtualxid" | blank | blank | "8/18417" | "8/18417" | 25475 | ExclusiveLock" | TRUE

你的“trip”變量會產生一個數組,這就是它返回BigDecimal的原因

你可以使用這種方法:

1)表示從ActiveRecord返回的第一個值

trip = MonthlyTrip.find(3).first
result = trip.al

要么

2)循環在數據庫中找到的每個元素。

trip = MonthlyTrip.find(3)trip.each do | t | 把t.al結束


[英]#<ActiveRecord::AssociationRelation Active Record Query Ruby on Rails

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM