I’ve got following method in User model
def get_employees
@employees = []
groups.each do |i|
@employees << Group.find(i).employees
end
@employees
end
This is what the console prints when I call this method:
> >> User.find(4).get_employees
> => [[#<Employee id: 4, first_name: "test", last_name: "test1",
> email_address: "test@gmail.com",
> created_at: "2010-08-25 04:23:02",
> updated_at: "2010-08-25 04:23:02">,
> #<Employee id: 5, first_name: "hello", last_name: "hello1", email_address:
> "hello@gmail.com", created_at:
> "2010-08-25 04:51:37", updated_at:
> "2010-08-25 04:51:37">]]
however, the following code does not work:
>> @user.get_employees.each{|i| p i.first_name}
NoMethodError: undefined method `first_name' for #<Class:0x9e372f0>
What do I need to do in order to get the first_name of the employees from the loop?
The
Group.find(i).employeescall returns an array, so yourget_employeesmethod is returning an array of arrays. Replacing the last line ofget_employeeswith@employees.flatten!should do the trick.