<%= form_with(model: @user, local: true) do |form| %>
<%= form.label :first_name %>
<%= form.text_field :first_name %>
<%= form.label :last_name %>
<%= form.text_field :last_name %>
<%= form.submit %>
<% end %>
<%= form_with(model: @user, local: false) do |form| %>
<%= form.label :first_name %>
<%= form.text_field :first_name %>
<%= form.label :last_name %>
<%= form.text_field :last_name %>
<%= form.submit %>
<% end %>
<%= form_with(model: @user, remote: true) do |form| %>
<%= form.label :first_name %>
<%= form.text_field :first_name %>
<%= form.label :last_name %>
<%= form.text_field :last_name %>
<%= form.submit %>
<% end %>
<%= form_with(model: @user, local: true, remote: true) do |form| %>
<%= form.label :first_name %>
<%= form.text_field :first_name %>
<%= form.label :last_name %>
<%= form.text_field :last_name %>
<%= form.submit %>
<% end %>
User.delete_all
User.destroy_all
Now select the correct statement(s) regarding the above queries:
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.
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.
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
destroy_all
returns the collection of objects that were destroyed but delete_all
only returns the number of rows were affected.
destroy_all
runs faster but delete_all
consumes the time
Article
and you have following queries to run:
Article.first(50).order(created_at: :desc)
Article.limit(50).order(created_at: :desc)
Now select the correct statement(s) regarding both queries:
Both will get executed successfully and returns the first 50 Articles and then apply ordering.
Only (I) will be executed successfully and (II) raise NoMethodError(undefined method order)
.
Only (II) will be executed successfully and (I) raise NoMethodError(undefined method order)
.
(I) will return an Array
instance but (II) will return an ActiveRecord::Relation
instance.
class Admin::UsersController < ApplicationController
def index
@users = User.all
end
end
Select the correct options for how you will define routes for this controller?
scope :admin do
resources :users
end
resources :admin do
resources :users
end
namespace :admin do
resources :users
end
All are correct
None of the above is correct
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:
before_create :set_default_status
before_save :set_default_status
before_commit :set_default_status
None of the above
class User < ApplicationRecord
validates :name, :address, presence: true
validates :contact, :email, presence: true, uniqueness: true
end
Now look at the following queries.
user1 = User.new(
email: 'user@example.com',
address: 'some address',
contact: '6979679670',
name: 'Examplen User')
user1.save!
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:
Both (I) and (II) will run successfully and create user1 and user2.
Only (I) will run successfully and create the user but (II) will not create user and will be rollbacked because of uniqueness validations.
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
None of the above
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:
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
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
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
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
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
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.
puts "#{self.name}"
puts name
puts "name"
Only A and B
All A, B, and C
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.
@user = User.find(params[:id])
@user.update(email: 'someemail@example.com').validate?(false)
@user = User.find(params[:id])
@user.update(email: 'someemail@example.com').validate(false)
@user = User.find(params[:id])
@user.update(email: 'someemail@example.com', validate: false)
@user = User.find(params[:id])
@user.email = 'someemail@example.com'
@user.save(validate: false)
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.
User.includes(:notes)
User.joins(:notes)
User.all
None of the above