Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have find_by in rails return multiple results

I am trying to return all groups created by a user. All of the groups are associated with the user id. When I run a find_by query it only returns the first result. Is there a way for it to return multiple? Thanks in advance

like image 790
rockyroadster555 Avatar asked Aug 21 '12 15:08

rockyroadster555


1 Answers

I'm writing a separate answer because I can't comment on James Lowrey's answer as I don't have 50 points.

find_all_by is deprecated (Ruby 4.2).

To get a list of active records from models, do:

Model.where(attribute_name: val)

For example, to find all records in Vehicle table (having column name "model_name") such that the value of model_name is "Audi", do

@vehicles = Vehicle.where(model_name: "Audi")

You can iterate through these like so:

<%  @vehicles.each do |vehicle| %>
like image 141
AarCee Avatar answered Sep 28 '22 07:09

AarCee