Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to your update I will propose this: Every time you delete a user who has posts and then try to list their posts <code>&lt;%= post.user.name %&gt;</code> will return nil because this post no longer has a user to refer to. You can either use the </p> <pre><code>has_many :posts, dependent: :destroy </code></pre> <p>in the User model as I suggested above, or you add some functionality to the deleting of users to prevent errors when trying to view their posts. Some choices are:</p> <p>When a user is deleted you can just delete everything but their name and id in that row of the user table, that will allow you to have <code>&lt;%= post.user.name %&gt;</code> return a value. </p> <p>When a user is deleted you can delete everything in their User table info and add "-user-deleted" after it kind of like what Tumblr does when a user deactivates their account and their name shows up in a post on some other Tumblr. </p> <p>You can create a User in the DB called "user-deleted" and have that user inherit every post from a user who is deleted. In the User controller add something in your <code>delete</code> action to change the user_id field in their posts to the "user-deleted" account.</p> <p>Or finally you could just check to see if post.user returns a value, and if not just print something like "The user no longer exists in the system"</p> <p>This is all dependent of course on if you even want to allow users to be deleted but still keep their posts. That's certainly a likely case if this is more than just a class project or something you are doing just to learn Rails. Hope this helps. It certainly got me thinking about the pitfalls in allowing users to delete their profiles without having something in place to clean up after them.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload