Posts

before_save & before_create

  before_save   is called every time an object is saved. So for new and existing objects. (create and update action) before_create  only before creation. So only for new objects (create action)

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) Student.find(4)  will raise an error:  ActiveRecord::RecordNotFound: Couldn't find Student with id=4 Student.find_by_id(4)  will return  nil  and will not raise an error.

Mention the differences between the observers and callbacks in ruby on rails.

  Following are the differences between observers and callbacks in ruby on rails: – Rails observers:  these are same as callbacks but are used when the method is not directly associated to the life cycle of the object. It lives for a longer duration of time and can be attached or detached at any time. Rails callback:  the callback methods can only be called at only certain points of time in the life cycle of an object like validation, creation, updating, deletion, etc. Unlike the rails observers, the rails callback lives for only a short period of time.

What do you understand by rails migration and what it can do?

With the help of rails migration, we can make changes in the database schema which makes it possible to use the version control system for synchronization of those things with the actual code. Rails migration can perform the following things: It helps in creating the table for the database. Dropping the table is possible with the help of this. Renaming of the table is possible. We can even add columns. Renaming of the columns can be done. Columns can be changed too. We can remove the columns and various other things can be done.  

Explain the role of sub directory app/controllers and app/helpers.

App/controllers:  in this, a web request is made by the user which is handled by the controller. Rails make use of this controller sub directory for finding the controller class. App/helpers:  some helper classes are present in the helper sub directory which is used for assisting the view, model and the controller classes present in it. Controller: The rails controller works as the main logical centre of the application. With the help of this user, views, and models can interact with each other. Routing of external requests to internal actions is possible. It can handle the URL very well. It helps in regulating helper modules which extends the capabilities of view templates. It also regulates the session which gives user an impression of an on-going interaction with any application.  

How can the filter methods in ruby on rails be protected?

  Some of the methods are listed below which are used for the protection of filters: – Public:  any external class or method that uses the same class in which it is defined can access this method. Protected:  these can be used only within the class in which they are defined and the classes that inherit from the class in which they are defined. Private:  these can be accessed only within the class in which they are defined.

What are the different filters used in ruby on rails?

  The methods which are used before and after the action of the controller method are executed. It ensures that the code runs with the given action method which is called. Three types of filters are supported by rails. They are: Before filters:  these filters are executed before the execution of the code in the action controller. After filters:  these filters are executed after the execution of the code in the action controller. Around filters:  these are executed both before and after the execution of the code present in the action controller.