Ruby On Rails

Q1: What does ruby name refer to? – Ruby on Rails
A: Ruby names refer to the classes, variables, methods, constants and modules that help in the creation of the program.
These names are reserved such that they can’t be used anywhere else and for any other purpose. The name that is used can be in lower or upper case, letter, number or an underscore.

Q2: What is difference between Static and Dynamic Scaffolding?
A:
a) Dynamic Scaffolding doesn’t require the database to be integrated as it gets created in runtime.
Static Scaffolding requires the database to, be migrated.
b) Dynamic Scaffolding allows the generation of new, edit and deletes methods for the use in application.
Static Scaffolding doesn’t allow any such generation to, take place.
c) Dynamic Scaffolding is used to give the model name that is being used with the function like: scaffold: model_name.
Static Scaffolding requires manual entry in the command and it is given as:
Script/generate scaffold Post title: string content:text category_id: integer
d) Dynamic Scaffolding automatically generates the entire content and user interface at runtime.
Static Scaffolding requires the insertion of the command to, generate the data with their fields.

Q3: What is the role of MVC architecture in Ruby on Rails?
A: MVC (Model-View-Controller) is the architecture that provides flexibility and scalability of the applications.
It is almost having the same concept in any other language like PHP, Perl or Python. It is one of the major used architecture involved today due to its simplicity.
Controller is the main part in this kind of architecture where it handles the request that is coming from another controller.
Controller contacts the view and passes on the request to the view and it also interacts with the model to define the type of request.
Model is responsible for interacting with the database and provides the responses to the controller.
Controller takes the response and gives the response in the output form to the user that has made the request.

Q4: What are the different components of Rails?
A: a) Action Controller: it is the component that manages all other controllers and process the incoming request to the Rails application.
It extracts the parameters and dispatches the response when an action is performed on the application.
It provides services like session management, template rendering and redirect management.
b) Action View: it manages the views of the Rails application and it creates the output in both HTML and XML format.
It also provides the management of the templates and gives the AJAX support that is being used with the application.
c) Active Record: It provides the base platform for the models and gets used in the Rails application. It provides the database independence, CRUID functionality, search capability and setting the relationship between different models.
d) Action Mailer: It is a framework that provides email services to build the platform on which flexible templates can be implemented.

Q5: What are the components defined in the model from MVC architecture?
A:
a) Validations: this is one of the very essential components and it defines the validations that are being put up on the input type of stream like validate_presence_of, format_of, etc.
b) Relationship: this is another type of component that describe the relationship between different types of components and it shows the relationship in the form of has_one, has_many, etc.
c) Callbacks: this is essential when it comes to respond after the failure and it allows the application to have certain functionality during failure. This can be given as before_save, after_save, etc.
d) Validation group settings: allow users to define the installed plugin settings.
e) Active record association relationship: allows current records to be actively having the relationship between one another.

Q6: What is the main function of helpers in Ruby on Rails?
A: Helpers are the functionality provided by the View for the modules to provide the help classes in MVC architecture.
Helpers provide the method that automatically used in the view. It helps the controller to take the pre-defined classes given in helper.
Helpers provide shortcuts to display the code that are used in the programming of the application and used to have the views.
Helpers allows user to see the structure of the output if the view file is having the less classes and written in a clear manner.
Helper classes are used to provide the functionality that is required to create an application.

Q7: What is the use of $ in Ruby?
A: $ in Ruby is treated as a global variable and it is used in a class variable that starts with an @sign and followed by upper or lower case letters. The $ (global variable) is followed by the name character and it defines the scope of the variable that is to be used.

Q8: What is the function of ORM in Ruby on Rails?
A: ORM stands for Object Relationship Model, that models the classes and helps in setting a relationship between the existing models.
It allows the classes to be mapped to the table that is present in the database and objects get mapped to the rows in database table.
It shows the relationship that exists between the object and frame it using the model to display the output to the users.
It keeps the data in the database according to its relationships and performs the functions accordingly.
It maps the database in case of any updates and update for the same if the fields are changed or new values are entered.

Q9: What are the different types of association relationships in ROR?
A:
a) One-to-one: this is the kind of relationship that exists between one item to another item. For example: Dog->Owner.
b) One-to-many: this kind of relationship or association exists between one item to many other item. Teacher-> student.
c) Many-to-many: this relationship shows many items to many items association and the items are related to each other. Student->Student.

Q10: What is the difference between Symbol and String?
A: The symbol in Ruby on rails act the same way as the string but the difference is in their behaviors that are opposite to each other.
a) The difference remains in the object_id, memory and process time for both of them when used together at one time.
b) Strings are considered as mutable objects. Whereas, symbols, belongs to the category of immutable.
c) Symbols are used to show the values for the actions like equality or non-equality to test the symbols faster then the string values.
d) Strings objects are mutable so that it takes only the assignments to change the object information. Whereas, information of, immutable objects gets overwritten.

Q11: What is the purpose of load, auto_load, and require_relative in Ruby?
A:
a) Load allows the process or a method to be loaded in the memory and it actually processes the execution of the program used in a separate file.
It includes the classes, modules, methods and other files that executes in the current scope that is being defined.
It performs the inclusion operation and reprocesses the whole code every time the load is being called.
b) Auto_load: this initiates the method that is in hat file and allows the interpreter to call the method.
c) require_relative: allows the loading to take place of the local folders and files.

Q12: What is the difference between render and redirect?
A:
a) Redirect is a method that is used to issue the error message in case the page is not found or it issues a 302 to the browser. Whereas, render is a method used to create the content.
b) Redirect is used when the user needs to redirect its response to some other page or URL. Whereas, render method renders a page and generate a code of 200.
c) Redirect is used to tell the browser to issue a new request. Whereas, render only works in case the controller is being set up properly with the variables that needs to be rendered.
d) Redirect is used as:
redirect_to: controller => ‘users’, :action => ‘new’
Render is used as:
render: partial
render: new -> this will call the template named as new.rhtml without the need of redirecting it to the new action.

Q13: What is the directory structure of Rails?
A: The directory structure of Rails includes the following:
-app/ – this is the folder that consists of controller, models and views of the appliation.
-config/- this folder consists of the configuration of the application that consists of runtime rules, routes, databases, etc.
-db/- this folder consists the database schema that is being currently used. It also has the information regarding the database migration.
-doc/- this is the folder that consists of all the documentation that is required to be used with the application.
-lib/- this folder consists of the extended modules and the libraries that is being used by the application
-log/- this folder consists of the logs that is being generated during the execution of the application.
-public/- this folder consists of the CSS, images and JavaScript folder to be used in the public domain with the application.

Q14: What are class variables? How do you define them?
A: Class variables are created using the @@ prefix to denote the variable as class level.
It works just like any other variable, however in the case of inheritance it works more like a static variable that is accessed across all variable instances.

Q15: Does Ruby support constructors? How are they declared?
A: Constructors are supported in Ruby. They are declared as the method initialize, shown below. The initialize method gets called automatically when Album.new is called.

class Album
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
end

Q16: What is Rake?
A: Rake is a popular ruby gem that makes the job of running tasks simpler.
Rake is most often used for DB tasks, but it can be used for m
The common DB commands are:
rake db:migrate
rake db:reset

You can use cron to schedule rake tasks.
Sometimes you would create a dataloader.rake file and put it in the lib/tasks folder so that it can be used to populate the database on startup.

Leave a Reply

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