Some Ruby on Rails interview questions

 1) How flash message work in ruby on rails?

Ans. They are stored in your session store. The default since rails 2.0 is the cookie store, but check in config/initializers/session_store.rb to check if you're using something other than the default.

According to APIdock : ActionController/Flash, it is stored in a session.
Note that if sessions are disabled only flash.now will work.
When using flash.now, your values are not available in the next request.


2) What is the use of Asset-PipeLine in Rails?

Ans. Assets(css, scss, js, coffeescript, images etc), its fast executed in rails application because of asset pipeline when we compile it or when its compiled, it will generate a single source code file or a manifest file, so that's why assets are interpret fastly.


3)   Difference between find and find_by?

Ans. The find method is usually used to retrieve a row by ID: 

        Model.find(1)
        And it will throw ActiveRecord::RecordNotFound error if record not found.

find_by is used as a helper when you're searching for information within a column, and it maps to such with naming conventions. For instance, if you have a column named name in your database, you'd use the following syntax: 

        Model.find_by(name: "Bob")
        And it will return nil if record not found.


4) Difference between class and module.

Ans: Modules
    1) Modules are about providing methods that you can use across multiple classes - think about them as "libraries" (as you would see in a Rails app). 13) Difference between Map, Collect, Select, Inject, Detect, Each in Ruby.
For example, authentication and authorization systems are good examples of modules. Authentication systems work across multiple app-level classes (users are authenticated, sessions manage authentication, lots of other classes will act differently based on the auth state), so authentication systems act as shared APIs.

    2) Module can not be instantiated 

    3) modules are about functions.

Classes:
    1) Class can be instantiated   
    2) classes are about objects.

5) Suppose we have two model(User and Phone) and associated like this User has-one Phone, so you have to get All User those have phones.

Ans. There are many ways to get users

    1) User.joins(:phones) //most efficient way.

    2) User.where(id: Phone.pluck(:user_id))

6) Difference between App-Server and Web-Server?

7) Life cycle of Http-Request.

8) print each character of a string i.e "Rahul" => R,a,h,u,l

    Ans: "rahul".each_byte{|x| puts x.chr}

9) return first duplicate number from array i.e [1,2,5,8,1,3,3] => 1

    Ans: a.detect{ |e| a.count(e) > 1 }

10) error handing in rails

11) difference between include and exclude?

12) what are callback in rails?

14) what is n plus 1 query problem in rails
 Ans: click here

15) ruby include vs extend vs prepend 

16) lazy loading vs eager loading rails

17) Preload, Eagerload, Includes and Joins
  Ans: Click Here

18) rails principles
    Ans: Click Here

19) what is string interpolation?

    Ans: Click Here

20) Difference between single and double quotes?

    Ans: Click Here

21) What is ORM in ROR?

    Ans: Click Here

22) What is ActiveRecord?

    Ans: Click Here

23) What is Rack Middleware?

    Ans: Click Here

24) How to skip ActiveRecord Callback?

    Ans: Click Here Click Here

25) What is Strong Parameters?

    Ans: Click Here

26) If a = true and a.class then what will be output ?

    Ans: TrueClass

27) if a = false or true then what will be print in ruby-shell and what will be stored in a, and if a = false || true then what will be output?

Ans: in first case true will print and false will stored in a

in second case true will print and true will stored in a

its because of precedence || has higher precedence then =

28) Suppose we have 2 tables users and orders and users has many orders,
    1) Get all users who has place a orders
    2) Get all users who has not placed any orders
    3) Get a user who has place max number of orders
    4) Get User name and order count of all users

    Ans: 1) User.joins(:orders)
            2) User.joins("left join orders on users.id  = orders.user_id where orders.user_id IS NULL")
            3) select * from users where id = (select user_id from orders group by user_id order by count(*) desc limit 1)
            4) select users.name, (select count(*) from orders where users.id = orders.user_id) from users

29) what is CSRF token in rails?
    Ans: Click Here

30) what is difference between put and patch request?
    Ans: Click Here

31) What is Rack?
    Ans:  
Rack is the magic that lets any of these app servers run your Rails app. (Or Sinatra app, or Padrino app, or…)
You can think of Rack as a common language that Ruby web frameworks (like Rails) and app servers both speak. Because each side knows the same language, it means Rails can talk to Unicorn and Unicorn to Rails, without having either Rails or Unicorn know anything about the other.

Click Here Click Here

32) What is difference between resource and resources?
    Ans: Click Here

33) Web-server and App-server and how they all fit together.
    Ans: Click Here

33) Sort ['aa', 'dd', 'kk', 'zz', 'xx'] is array in ascending order i.e ['aa', 'dd', 'kk', 'xx', 'zz']
    Ans: 

class SelectionSort
  def abc a
    temp = 0
      (0...a.length).each do |i|
        (i+1...a.length).each do |j|
          if a[i] > a[j]
            temp = a[i]
            a[i] = a[j]
            a[j] = temp
          end
        end
      end
      return a
    end
  end

puts SelectionSort.new.abc(['aa', 'dd', 'kk', 'xx', 'zz'])

Comments

Popular posts from this blog

Wicked-PDF not showing images, 'wicked_pdf_image_tag' undefined