テーブルからアイテムを取得する方法 条件を使用して、質問列から値を取得したい。
@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