Users Rails Console


Collapse Content

The preliminary Rails Console challenge involved building a simple Rails console. Build off that earlier challenge and add additional features:

  • Create a User model with a name and email. It should have the same capabilities as the Post model:
    • User.create("name", "email") should create a user with the given name and email
    • User.find(id) should return the user with the specified id
    • User.last should return the last user created (that still exists)
    • Printing a User should print out the name and email of the user.
  • Let a User have many posts. Modify how a post is created so it takes in two parameters: the content of the post, and the ID of the User the post belongs to. Create a method .posts for a user which returns all the posts that belong to that user.
  • Create a destroy method for both Users and Posts. It should destroy the item with a given id.
  • ID's - The ID for an item should be assigned in order of when the item is created. The first item created should have an ID of 1, the second an ID of 2, etc. (The id of each item should not change when one item is destroyed.)

Sample Input

User.create("jim", "[email protected]")
User.create("sam", "[email protected]")
puts User.last
Post.create("first post", 1)
Post.create("second post", 2)
Post.create("world", 1)
puts Post.find(1)
puts Post.last
Post.destroy(3)
puts Post.last
Post.create("last post", 1)
puts User.find(1).posts
quit

Sample Output

User created
User created
sam [email protected]
Post created
Post created
Post created
first post
world
Post destroyed
second post
Post created
first post
last post

Note that the input puts User.find(1).posts produces two lines of output, since it prints two posts:

first post
last post

Challenge

Create the new features for the Rails console.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Contact Us
Sign in or email us at [email protected]