John Oggy
John Oggy

Reputation: 319

How get value from database ?

How get items from table ? I want get value from question column, using condition.

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

Now, I can get value from any column ? like this: @result.column_from_customers_table , right ?

Upvotes: 0

Views: 122

Answers (1)

jvnill
jvnill

Reputation: 29599

This is a common mistake for beginners. The code you have returns an ActiveRecord::Relation object and doesn't actually connect to your db yet. In order to get a record you have to loop through each one of the results or call .first on it in order to get the first matching result

# 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

Upvotes: 1

Related Questions