Seed file contradiction in terminal and pry console

219 views Asked by At

I have a small problem. I am making a simple project website which has 4 models (users, articles, discussions, comments).

The following associations have been set up:

Users have many discussions and comments, articles have many discussions (and comments through discussions). Comments belong to discussions and users, and discussions belong to articles.

In my seeds.rb file, I have made entries to be put into these tables, and with the correct associations. After making several entries, I have included a

puts "Created #{User.all.length} users..."

just so I can see in terminal that everything is functioning correctly.

When using rails db:seed, the puts message will output the correct number for users and articles, but will say 0 for comments and arguments.

However, I can check in rails dbconsole that SELECT * FROM comments; will return all the entries from the seed file.

In the pry-rails console, associations also seem to be in place. A command such as Comment.first.user will output the expected association as per the seed file.

Why does the line puts "Created #{Comment.all.length} comments..." output "Created 0 comments..." in terminal, but Comment.all.length in the pry console output 8 (the number of comments created in seed file)?

1

There are 1 answers

0
achacttn On BEST ANSWER

The problem was fixed by putting the puts "Created #{User.all.length} users..." line after associations were made.