Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Ethan Moffat
Ethan Moffat
184 Points

How can I display different code if my posts table is empty?

I am here because I am trying to display my blog data on the landing page however, I just cleared the testing and now it gives me a NoMethodError. Obviously it's cause the posts table is empty.

I am curious if there is anyway to display static code when the database table is empty.

Ethan Moffat
Ethan Moffat
184 Points

Nevermind, I ended up just doing a simple if statement to check if the database table is empty by doing

    <%= if Post.any? %>
          #when table in not empty
   <% else %>
          # Static code
   <% end %>

Hope this helps others in the future!