In my rails app, I'm using Her gem for my models. One of my models, team, has_many of another model, players. In the team controller, I'm iterating over the team's players with @team.players.each do |player|. This is a problem in cases where the team has been created, but does not have any players in the database because @team.players does not exist. If I try to do something like if @team.players or if @team.players.size, it throws an error from my Api saying no rows were returned.
How can I force players to be initialized whether or not there are any records in the database (or otherwise handle its nonexistence)?
Aucun commentaire:
Enregistrer un commentaire