Search code examples
ruby-on-railsruby-on-rails-3postgresqlpg

Rails shows rows from model, which aren't created


I have this part of code:

<% current_user.meta_accounts.each do |meta_account| %>
    <%= content_tag(:li, link_to(meta_account.name, 'javascript:void(0)')) %>
<% end %>

So, I want Rails to show all my MetaAccounts in list, but I get this:

<li><a href="javascript:void(0)">Wallet</a></li>
<li><a href="javascript:void(0)">Credit card</a></li>
<li><a href="javascript:void(0)">Debit card</a></li>
<li><a href="javascript:void(0)">Debts</a></li>
<li><a href="javascript:void(0)">javascript:void(0)</a></li> #This is the problem

So, it also shows me MetaAccount, which isn't created yet.

In my MetaAccounts table I have this. I'm using Postgres.

enter image description here

So, it also shows me the last row, where row_number is *. I don't know why, and how to avoid this.

Thanks for any help!


Solution

  • Try:

    <% current_user.meta_accounts.select(&:persisted?).each do |meta_account| %>
      <%= content_tag(:li, link_to(meta_account.name, 'javascript:void(0)')) %>
    <% end %>