Users Rails Console
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
User.lastshould 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
.postsfor a user which returns all the posts that belong to that user.
- Create a
destroymethod 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.)
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
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