0

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)?

achacttn
  • 163
  • 2
  • 9
  • 1
    try to use `count` instead of `length` (you can check this answer [here](https://stackoverflow.com/a/6083229/9541423)) – Sovalina May 19 '18 at 14:04
  • @sovalina The post you linked was very informative. However, I have tried running `rails db:seed` with `count` and `size`, but am getting the same result (not counted in the terminal, but exists in database). – achacttn May 19 '18 at 14:11

1 Answers1

0

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

achacttn
  • 163
  • 2
  • 9