2016-01-19 69 views
-5

我在這裏面臨一個小問題。 我取使用下面的代碼數據的數據,布爾驗證

class CreateGrounddetails < ActiveRecord::Migration 
def change 
    create_table :grounddetails do |t| 
    t.string :name 
    t.datetime :working_hours 
    t.string :address 
    t.string :contact_no 
    t.string :email 
    t.integer :number_of_grounds 
    t.text :description 
    t.boolean :featured_ground #Featured 
    t.timestamps null: false 
    end 
end 
end 

我有一個存儲在域「featured_ground」一個布爾值,現在我想只獲取其是否有「featured_ground「真」值數據」。 我該如何做到這一點?

預先感謝您。

+0

檢查生成的SQL我想有地面我period..how能做到這一點是一定時間真? –

回答

1

你可以做

Grounddetail.where(featured_ground: true) 

它給你預期的記錄。

+1

非常感謝...我很沮喪,因爲如何做到這一點..感謝:) –

+0

嘿謝謝這個答案很多..現在我想特點地面是真的在一定的時間.. ..我可以做到這一點嗎? –

0

下面應該這樣做,假設您的模型將被命名爲Grounddetail

Grounddetail.where(:featured_ground => true) 

您可以通過使用

Grounddetail.where(:featured_ground => true).to_sql 
1
Grounddetail.where(featured_ground: true) 

OR

Grounddetail.where(:featured_ground => true) 
+0

這不提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 - [來自評論](/ review/low-quality-posts/10943334) –

+0

如果這不是一個答案,那麼它是什麼? –

+1

你的答案被標記爲「太短」,實際上不僅僅是評論。我建議你添加更多的細節,讓用戶理解「爲什麼」它可能是一個好的或者最好的。讓用戶瞭解原因可以幫助他們處理其他類似情況。 –