0
如何從表中獲取物品?我想從問題列中獲取值,使用條件。如何從數據庫中獲取價值?
@result = Customers.where(:name => session[:username], :email => session[:useremail])
現在,我可以從任何列獲得價值嗎?像這樣:@ result.column_from_customers_table,對不對?
如何從表中獲取物品?我想從問題列中獲取值,使用條件。如何從數據庫中獲取價值?
@result = Customers.where(:name => session[:username], :email => session[:useremail])
現在,我可以從任何列獲得價值嗎?像這樣:@ result.column_from_customers_table,對不對?
這是初學者常犯的錯誤。你有的代碼返回一個ActiveRecord::Relation
對象,但實際上並沒有連接到你的數據庫。爲了得到一個記錄,你必須通過每一個結果循環或者調用.first
就可以得到第一個匹配結果
# returns an ActiveRecord::Relation object
@results = Customers.where(:name => session[:username], :email => session[:useremail])
# returns the first matching record
@object = @results.first
# then you can call the column names on @object
@object.name
@object.email
# looping through the results
@results.each do |object|
puts object.name
puts object.email
end