Ruby on Rails Interview Questions and Answers

Ruby on Rails Intevriew Questions and Answers

In case you’re searching for Ruby on Rails Interview Questions and Answers for Experienced or Freshers, you are at the correct place. Gangboard offers Advanced Ruby on Rails Interview Questions and Answers that assist you in splitting your Ruby on Rails Interview and procure dream vocation as Ruby on Rails Developer.

Q1)  Does ruby support multiple inheritance ? How ruby solves multiple inheritance ?

Answer: No. It solves by using mixins. We use include to solve multiple inheritance.

class Land
def jump
class Water
def swim
class Frog
include Land
include Water
# all methods of Land and Water are available in Frog

Q2) How is extend different from include in ruby?

Answer: When methods are extended, we can access them using class variables. When we include the method we can access them using instance variables.

Q3) Is rails rack based application? What is Rack in rails?

Answer: Yes. Rack is a bridge between the application and the server to communicate. The communication happens through env hash.

Q4) What is ORM?

Answer: ORM is object relational mapping.

  1. Class in application refers to table names.
  2. Object of a class in database refers to a row
  3. Attributes in database refers to a columns of the table.

Q5) Explain has_many through association with an real time example ?

Answer: When Patient calls up doctor for an appointment, he gets an Appointment receipt. A doctor may have many patients for an appointment. A patient also may have consulted many doctors with an appointment. So, doctor has_many patients through appointments. patietns has_many doctors through appointments.

Q6) How to add a column in a table in rails ?

Answer: Run rails generate migration —your_file_name—-
def change
add_column :—table_name—, –column_name—, :—column_type—

Q7) How rails uses ORM?

Answer: It uses through active record.

Q8) What’s the difference between map and each in ruby?

Answer: Each loops through an array and returns the main array, but map returns the resultant array.

Q9) Explain includes in active record?

Answer: Uses eager loading and optimally used when accessing each member attributes from the associated records. Performs LEFT OUTER JOIN

Q10) Explain joins in active record?

Answer: Uses lazy loading and optimally used when filtering results on condition with associated records.


Q11) For avoiding n + 1 queries, should we use includes or joins?

Answer: includes.

Q12) What is nilClass error?

Answer: nil is nothingness in ruby. When calling a method not applicable to nil gives nilClass error.

NoMethodError: undefined method `next’ for nil:NilClass

Q13) How can we avoid nilclass errors?

Answer: By using ‘:try’ for exception handling.

Q14) How does rescue work in ruby?

Answer: rescue is used with begin block. When there’s an error in begin block, control flows through rescue. When there is no begin block, def acts an begin block.

Ex: def get_name
# error occurs here
# control flow here if error occurs
# render appropriate error messages in rescue block

Q15) How render and redirect_to available in controllers and not in models?

Answer: Because render and redirect_to are defined in ActionView::Helpers and accessible from action Controller.

Q16) What is difference between nil? and present? and which is more optimized? nil? checks only if an object is nil? or not. But present? checks if object has some value or not. present? checks value in hash and array too.

Answer: 2.3.1 :017 > a = nil

=> nil

2.3.1 :018 > a.nil?

=> true

2.3.1 :019 > a.present?

=> false

2.3.1 :023 > b = [1, 2, 3]

=> [1, 2, 3]

2.3.1 :024 > b.nil?

=> false

2.3.1 :025 > b.present?

=> true

Q17) Can we loop into an hash in ruby? Give an example?

Answer: Yes. If h is an hash, then we can loop like this:

h.each do |key, value|

# your code here


Q18) What is rvm and what is the command to see all gemsets in rvm?

Answer: rvm is ruby version manager. rvm list is the command to see all gemsets.

Q19) How to embed ruby code into html?

Answer: Need to rename the file with html.erb extension,'<% %>’ will execute ruby code and ‘<%= %>’ will execute ruby code and print the result.

Q20) How to precompile assets in rails?

Answer: We need to add the asset file in ‘/config/initializers/assets.rb’ and re-run the server.

Q21) How to include javascript code in the view using rails?

Answer: using javascript_include_tag.

<%= javascript_include_tag “#{params[:controller]}_#{params[:action]}”, “data-turbolinks-track” => true %>

Q22) How to use strong parameters in rails?

Answer: We need to use ‘require’ and ‘permit’ to allow the required attributes.

Ex: params.require(:student).permit(:name, section, marks)

Q23) Are ‘params’, ‘require’ and ‘permit’ available in model in rails?

Answer: No. These are available in ActionContoller::Parameters, which are accessible from ApplicaitonController and it’s inherited controllers.

Q24) What does rake db:migrate do?

Answer: It executes the migration files staged and commits into the schema of database.

Q25) How to get all route path in the application?

Answer: Run rake routes

Q26) How class and instance variables are defined in controller?

Answer: @@some_variable -> class variable

@some_variable -> instance variable

Q27) Mention some filters used in controllers?

Answer: before_action, before_filter, after_action, around_action

Q28) How to get current datetime in ruby?


=> Tue, 25 Dec 2018 17:58:33 +0530

Q29) How to convert IST datetime into USA time.

Answer: By using time_zone(—time_zone_attribute—)‘Central Time (US & Canada)’)

=> Tue, 25 Dec 2018 06:31:54 CST -06:00

Q30) How to convert an datetime object into date object?

Answer: By using ‘to_date’

=> Tue, 25 Dec 2018

Q31) What is asynchronous call?

Answer: When the separate thread is evolved from main thread to send a request and not waiting for it’s response is asynchronous call.

Q32) How to configure timezone for the entire application?

Answer: In config/application.rb change:

config.time_zone = ‘Asia/Kolkata’

Q33) When is ActiveRecord::Base.transactions used?

Answer: Placing active record queries in this block ensures all the transactions passes. If anyone fails then all previous database transactions in this transaction block gets rollback.

Q34) How to revert migration files?

Answer: By using rake db:rollback. We can also specify which migration version to rollback.

Q35) Is this below code valid?

Answer: def get_name(first_name, last_name = nil)
puts “#{first_name} #{last_name}”
Yes. first_name is mandatory whereas last_name is optional. Prints ‘srinidhi’.

Q36) What is CSRF and how rails protects our application?

Answer: Cross site request forgery. protect_from_forgery with: :exception in applicaton controller protects other apps to eavesdrop into our application through API calls. Post requests within our application will hava CSRF token which authenticates each requests.

Q37) What is an CSV?

Answer: CSV is an file with comma separated values, which can be extracted into an excel.

Q38) What is rake task?

Answer: It’s a custom task in ruby excecutable at different application environment. It’s an interface to make custom changes to the application.

Q39) How to get all unique values in an array?

Answer: arr = [1, 2, 3, 3 ,4 ,7, 7]

b = arr.uniq


The bang(!) assigns the result to arr variable.

Q40) What’s the difference between create and create! in rails?

Answer: create is to add an row in table and rolls back when there is an exception.

create! does the same as create but raises an application.

Q41) When is deep_symbolize_keys in params?

Answer: Which means params having keys with symbol as well as string is accessible.

Ex: params[:name] gives same value as params[‘name’]

Q42) Difference between a symbol and a string?

Answer: String variable can be changed once initiated. But symbol cannot be changed.

2.3.1 :051 > ‘qwe’ + ‘qwe’

=> “qweqwe”

2.3.1 :052 > :qwe + ‘qwe’

NoMethodError: undefined method `+’ for :qwe:Symbol

Q43) How to check element exists in an array? Use include?.

Answer: 2.3.1 :054 > a = [1, 2,  3, 4]

=> [1, 2, 3, 4]

2.3.1 :055 > a.include?(1)

=> true

Q44) How to access rails sql console?

Answer: rails dbconsole

psql (9.6.2).


Q45) What is ActionDispatcher?

Answer: Responsible for setting route path for the application. When we add route path in routes.rb, definite path to corresponding controller action is set using ActionDispatcher.

GET, POST, PUT, DELETE are some states.

Q46) Give hierarchy of ruby method lookup?

Answer: Precedence of ruby methods goes like this:

  1. ‘singleton’ methods
  2. ‘extend’ methods
  3. ‘prepend’ methods
  4. ‘instance’ methods
  5. ‘include’ methods
  6. ‘inherited’ methods

Leave a Reply

Your email address will not be published. Required fields are marked *

Looking for Online Training