Ruby on Rails MCQs

1. Which of the following form will submit the users form as AJAX?
A.
                    
                      
                        <%= form_with(model: @user, local: true) do |form| %>
                          <div class="field">
                            <%= form.label :first_name %><br>
                            <%= form.text_field :first_name %>
                          </div>

                          <div class="field">
                            <%= form.label :last_name %><br>
                            <%= form.text_field :last_name %>
                          </div>

                          <div class="actions">
                          <%= form.submit %>
                        <% end %>
                      
                    
                  
B.
                    
                      
                        <%= form_with(model: @user, local: false) do |form| %>
                          <div class="field">
                            <%= form.label :first_name %><br>
                            <%= form.text_field :first_name %>
                          </div>

                          <div class="field">
                            <%= form.label :last_name %><br>
                            <%= form.text_field :last_name %>
                          </div>

                          <div class="actions">
                          <%= form.submit %>
                        <% end %>
                      
                    
                  
C.
                    
                      
                        <%= form_with(model: @user, remote: true) do |form| %>
                          <div class="field">
                            <%= form.label :first_name %><br>
                            <%= form.text_field :first_name %>
                          </div>

                          <div class="field">
                            <%= form.label :last_name %><br>
                            <%= form.text_field :last_name %>
                          </div>

                          <div class="actions">
                          <%= form.submit %>
                        <% end %>
                      
                    
                  
D.
                    
                      
                        <%= form_with(model: @user, local: true, remote: true) do |form| %>
                          <div class="field">
                            <%= form.label :first_name %><br>
                            <%= form.text_field :first_name %>
                          </div>

                          <div class="field">
                            <%= form.label :last_name %><br>
                            <%= form.text_field :last_name %>
                          </div>

                          <div class="actions">
                          <%= form.submit %>
                        <% end %>
                      
                    
                  
2. Considering that you have a User model(without association to any other model) in an application and you want to delete all User records from database, you can do this by following queries:
                
                  User.delete_all
                
              
                
                  User.destroy_all
                
              

Now select the correct statement(s) regarding the above queries:

A.

Both delete all the users from the database but delete_all will instantiate each user record first and then delete and destroy_all will delete all the users but does not instantiate them before delete.

B.

Both delete all the users from the database but destroy_all will instantiate each user record first and then delete them, and delete_all will delete all the users but does not instantiate them before delete.

C.

delete_all will generate at least one SQL DELETE query per user record but destroy_all will execute only one SQL DELETE query to delete all user records

D.

destroy_all returns the collection of objects that were destroyed but delete_all only returns the number of rows were affected.

E.

destroy_all runs faster but delete_all consumes the time

3. You have a model in your rails app let's say Article and you have following queries to run:
(I).
                    
                      Article.first(50).order(created_at: :desc)
                    
                  
(II).
                    
                      Article.limit(50).order(created_at: :desc)
                    
                  

Now select the correct statement(s) regarding both queries:

A.

Both will get executed successfully and returns the first 50 Articles and then apply ordering.

B.

Only (I) will be executed successfully and (II) raise NoMethodError(undefined method order).

C.

Only (II) will be executed successfully and (I) raise NoMethodError(undefined method order).

D.

(I) will return an Array instance but (II) will return an ActiveRecord::Relation instance.

4. Consider following controller snapshot:
                
                  class Admin::UsersController < ApplicationController

                    def index
                      @users = User.all
                    end

                  end
                
              

Select the correct options for how you will define routes for this controller?

A.
                    
                      scope :admin do 
                        resources :users
                      end
                    
                  
B.
                    
                      resources :admin do 
                        resources :users
                      end
                    
                  
C.
                    
                      namespace :admin do 
                        resources :users
                      end
                    
                  
D.

All are correct

E.

None of the above is correct

5. I have a Task model in my rails app which has two attributes title and status. Now look at the Task model below:
                
                  class Task < ApplicationRecord
                    validates :title, presence: true
                    
                    # I want to use this method to set the default status of task only 
                    # before saving a new task instance in database 
                    def set_default_status
                      self.status = 'pending'
                    end

                  end
                
              

Which of the following callback you will use:

A.
                    
                      before_create :set_default_status
                    
                  
B.
                    
                      before_save :set_default_status
                    
                  
C.
                    
                      before_commit :set_default_status
                    
                  
D.

None of the above

6. Consider the following User model in a rails application:
                
                  class User < ApplicationRecord
                    validates :name, :address, presence: true
                    validates :contact, :email, presence: true, uniqueness: true
                  end
                
              

Now look at the following queries.

(I).
                    
                      user1 = User.new(
                                email: 'user@example.com',
                                address: 'some address',
                                contact: '6979679670',
                                name: 'Examplen User')
                      user1.save!
                    
                  
(II).
                    
                      user2 = User.new(
                                email: 'user@example.com',
                                address: 'some address',
                                contact: '6979679670',
                                name: 'Examplen User')
                      user2.save!
                    
                  

When you execute both queries what will happen? Please select the correct option:

A.

Both (I) and (II) will run successfully and create user1 and user2.

B.

Only (I) will run successfully and create the user but (II) will not create user and will be rollbacked because of uniqueness validations.

C.

Only (I) will run successfully and create the user but (II) will not create user and will be rollbacked because of uniqueness validations. And raise an ActiveRecord::RecordInvalid exception

D.

None of the above

7. Consider following callback action in article_controller.rb.
                
                  def set_article
                    @article = Article.find(params[:id])
                  end
                
              

This action will find an article for the supplied id in params. But if the Article record with supplied id is not found, this breaks the app and returns a 404 error. But this is not a good thing. You will be asked to handle the 404 error and instead of breaking the app display some meaningful message to users so that they can understand what wrong they have done.

Which of the following method you think is correct for this usecase:

A.
                    
                      def set_article
                        @article = Article.find(params[:id])
                      rescue ApplicationRecord::RecordNotFound
                        redirect_to root_path, notice: 'The article you are looking for does not exists'
                      end
                    
                  
B.
                    
                      def set_article
                        @article = Article.find(params[:id])
                      rescue ActiveRecord::RecordNotExists
                        redirect_to root_path, notice: 'The article you are looking for does not exists'
                      end
                    
                  
C.
                    
                      def set_article
                        @article = Article.find(params[:id])
                      rescue ApplicationRecord::RecordNotExists
                        redirect_to root_path, notice: 'The article you are looking for does not exists'
                      end
                    
                  
D.
                    
                      def set_article
                        @article = Article.find(params[:id])
                      rescue ActiveRecord::RecordNotFound
                        redirect_to root_path, notice: 'The article you are looking for does not exists'
                      end
                    
                  
E.
                    
                      def set_article
                        @article = Article.find(params[:id])
                      rescue Article::RecordNotFound
                        redirect_to root_path, notice: 'The article you are looking for does not exists'
                      end
                    
                  
8. You have been given a model name Article, which has title and body attribute. There is also a title method in the Article model. Look at the below model snapshot:
                
                  class Article < ApplicationRecord
                    validates :title, :body, presence: true

                    def title
                      # Do something here for raising System Stack Error    
                    end
                  end
                
              

Here I ask you to raise an Exception for SystemStackError: stack level too deep. For this you have to write a line in title method.

Which of the following option(s) is correct for raising the exception. Apply that all.

A.
                    
                      puts "#{self.name}"
                    
                  
B.
                    
                      puts name
                    
                  
C.
                    
                      puts "name"
                    
                  
D.

Only A and B

E.

All A, B, and C

9. Please look into the following model snapshot
                
                  class User < ApplicationRecord
                    validates :first_name, :last_name, :contact, :email, presence: true, on: :update 
                  end
                
              

For all the users values of first_name, last_name, contact fields are empty. I just want to update the value of the email field. But when I am trying to update email I am getting a validation error.

Which of the following query(s) will be executed successfully. Select all that apply.

A.
                    
                      @user = User.find(params[:id])
                      @user.update(email: 'someemail@example.com').validate?(false)
                    
                  
B.
                    
                      @user = User.find(params[:id])
                      @user.update(email: 'someemail@example.com').validate(false)
                    
                  
C.
                    
                      @user = User.find(params[:id])
                      @user.update(email: 'someemail@example.com', validate: false)
                    
                  
D.
                    
                      @user = User.find(params[:id])
                      @user.email = 'someemail@example.com'
                      @user.save(validate: false)
                    
                  
10. Consider the following association between User and Note model:
                
                  class User < ApplicationRecord
                    has_many :notes, dependent: :destroy
                  end

                  class Note < ApplicationRecord
                    belongs :user
                  end
                
              

Which of the following query will return only the users who have at least one note.

A.
                    
                      User.includes(:notes)
                    
                  
B.
                    
                      User.joins(:notes)
                    
                  
C.
                    
                      User.all
                    
                  
D.

None of the above