Skip to content

Instantly share code, notes, and snippets.

@Epigene
Created March 13, 2017 12:26
Show Gist options
  • Save Epigene/2b97e6cd63eafb6be3a62f1ffbe04a77 to your computer and use it in GitHub Desktop.
Save Epigene/2b97e6cd63eafb6be3a62f1ffbe04a77 to your computer and use it in GitHub Desktop.
Warmup rails questions

1. What is the difference between Ruby’s Hash and ActiveSupport’s HashWithIndifferentAccess?

2. What’s the issue with the controller code below? How would you fix it?

class CommentsController < ApplicationController
  def users_comments
    posts = Post.all
    comments = posts.map(&:comments).flatten
    @user_comments = comments.select do |comment|
      comment.author.username == params[:username]
    end
  end
end

3. How would you define a Person model so that any Person can be assigned as the parent of another Person (as demonstrated in the Rails console below)? What would the migration look like?

irb(main):001:0> john = Person.create(name: "John")
irb(main):002:0> jim = Person.create(name: "Jim", parent: john)
irb(main):003:0> bob = Person.create(name: "Bob", parent: john)
irb(main):004:0> john.children.map(&:name)
=> ["Jim", "Bob"]

4. Suppose we have a Student with id=”4”. If we delete the Student with id=”4”, what will be the result of the following queries:

Student.find(4)
Student.find_by_id(4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment