0

テーブルからアイテムを取得する方法 条件を使用して、質問列から値を取得したい。

@result = Customers.where(:name => session[:username], :email => session[:useremail])

これで、任意の列から値を取得できますか? このように: @result.column_from_customers_table ですね。

4

1 に答える 1

1

これは初心者によくある間違いです。あなたが持っているコードは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
于 2013-04-24T10:53:07.023 に答える