hi i am attempting to do a find_by_id, though when calling this method if the id doesnt exist im getting nil class
if current_user.admin?
@schedules = Schedule.all
elsif current_user.team_id?
@schedules = Schedule.find_by_team_id(current_user[:team_id])
end
error
NoMethodError in Schedules#index
Showing /Users//Sites/Rails/*/app/views/schedules/index.html.haml where line #44 raised:
undefined method `each’ for nil:NilClass
Extracted source (around line #44):
41: %th Location
42: %th{:style => "color: #675100;background: url(\"/img/sprites/alertboxes/bg-warning.png\") repeat-x #ffe68b;"} Manager
43: %tbody
44: - @schedules.each do |schedule|
45: %tr
46: %td= event_display(schedule.event)
47: %td
This is the normal behaviour if you are using find_by_ will return nil if no record is found for that attribute. Also note that find_by_ returns only a record not an Enumerable. you should you where(…) or find_all_by_ instead