Bad Programmer
Bad Programmer

Reputation: 952

Working With Database Results in RoR

I am an RoR noob. I am selecting all records from a model and saving it into a variable, but am having difficulty determining how to grab a specific field.

For example, I have a model called Stats that maps to a stats view in a MySQL database. The view has the following columns: pet, height, weight.

This is how I am grabbing the data: @dog_distribution = Distribution.where(pet: 'dog').

Assume that 3 records are returned, how can I grab the weight value of the first record?

Upvotes: 0

Views: 30

Answers (1)

Manish Yadav
Manish Yadav

Reputation: 91

To view all weight values, you may try something like this

@dog_distribution = Distribution.where(pet: 'dog').pluck(:weight)

This will return array with weight of each element.

If you need value of only first element, try something like this

@dog_distribution = Distribution.where(pet: 'dog').first.weight

Upvotes: 1

Related Questions