Ruby on Rails Interview Questions and Answers
by Venkatesan M, on May 17, 2017 9:24:49 AM
Q1. In which Programming language was Ruby written?
Ans: Ruby was written in C language and Ruby on Rails written in Ruby.
Q2. Mention all the naming conventions in ruby on rails.
Ans: Following is a list of some naming conventions used in ruby on rails: –
- Variables– it is used for the declaration of variables in which all the letters should be in lowercase and the words should be separated by underscore.
- Class and module– a class is declared which encapsulates every function. The class or module name can be written in mixed case and no underscores are used. Every word should start with an uppercase letter.
- Database table– a table is made for storing the data. It is the same as the naming conventions for variables.
- Model– mixed case is used and have singular with table name.
- Controller– names are represented in plural form.
Q3. What is ORM in Rails?
Ans: ORM tends for Object-Relationship-Model, it means that your Classes are mapped to table in the database, and Objects are directly mapped to the rows in the table.
Q4. Why Ruby on Rails?
Ans: There are lots of advantage of using Ruby on Rails.
- DRY Principal( Don’t Repeat Yourself): It is a principle of software development aimed at reducing repetition of code. “Every piece of code must have a single, unambiguous representation within a system”
- Convention over Configuration: Most web development framework for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn’t need much configuration.
- Gems and Plugins:RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing ruby programs and library.
- Plugins: A Rails plugin is either an extension or a modification of the core framework. It provides a way for developers to share bleeding-edge ideas without hurting the stable code base. We need to decide if our plugin will be potentially shared across different Rails applications.
- Scaffolding: Scaffolding is a meta-programming method of building database-backend software application. It is a technique supported by MVC frameworks, in which programmer may write a specification, that describes how the application database may be used. There are two type of scaffolding:
- static: Static scaffolding takes 2 parameter i.e your controller name and model name.
- dynamic: In dynamic scaffolding you have to define controller and model one by one.
- Rack Support: Rake is a software task management tool. It allows you to specify tasks and describe dependencies as well as to group tasks in a namespace.
- Metaprogramming: Metaprogramming techniques use programs to write programs.
- Bundler: Bundler is a new concept introduced in Rails 3, which helps you to manage your gems for application. After specifying gem file, you need to do a bundle install.
Rest Support.
Action Mailer
Q5. Explain the features of ruby on rails.
Ans: Some of the features of ruby on rails are listed below-
- Meta-programming: code generation is used but for heavy lifting, it uses meta-programming.
- Active record: objects are saved to the database through this framework. It identifies the columns in a schema and binds them to your domain object.
- Scaffolding: it means it has the ability of creating temporary code automatically.
- Convention over configuration: much configuration is not needed if the naming convention is followed.
- Three environments: testing, development, and production are the three default environments in this framework.
- Built-in testing: test cases are used in this for writing and executing the codes.
Q6. How many Types of Associations Relationships does a Model has?
Ans: When you have more than one model in your rails application, you would need to create connection between those models. You can do this via associations. Active Record supports three types of associations:
- one-to-one : A one-to-one relationship exists when one item has exactly one of another item. For example, a person has exactly one birthday or a dog has exactly one owner.
- one-to-many : A one-to-many relationship exists when a single object can be a member of many other objects. For instance, one subject can have many books.
- many-to-many : A many-to-many relationship exists when the first object is related to one or more of a second object, and the second object is related to one or many of the first object.
You indicate these associations by adding declarations to your models: has_one, has_many, belongs_to, and has_and_belongs_to_many.
Q7. What is MVC? and How it Works?
Ans: MVC basically indicates Model-View-Controller. And MVC used by many languages like PHP, Perl, Python etc. Generally MVC works like this:
Request first comes to the controller, controller finds and appropriate view and interacts with model, model interacts with your database and send the response to controller then controller based on the response give the output parameter to view.
Q8. What do you understand by rails?
Ans: It is a web application framework which is written in ruby language and is developed by David Hansson. It is an open source ruby framework for the development of database backend web application. It consists of everything which is needed for creating a database driven web application with the help of model view controller pattern.
Q9. What is the Difference between Static and Dynamic Scaffolding?
Ans: The Syntax of Static Scaffold is like this:
ruby script/generate scaffold User Comment.
Where Comment is the model and User is your controller, So all n all static scaffold takes 2 parameter i.e your controller name and model name, whereas in dynamic scaffolding you have to define controller and model one by one.
Q10. How many types of relationships does a Model has?
Ans:
- has_one
- belongs_to
- has_many
- has_many :through
Q11. What are the different filters used in ruby on rails?
Ans: 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.
Q12. How you run your Rails Application without creating database ?
Ans: MYou can run application by uncomment the line in environment.rb
Path => rootpath conf/ environment.rb
# Skip frameworks you're not going to use (only works if using vendor/rails)
config.frameworks -= [ :action_web_service, :action_mailer,:active_record ].
Q13. What is Ruby Gems?
Ans: Ruby Gem is a software package, commonly called a "gem". Gem contains a packaged Ruby application or library. The Ruby Gems software itself allows you to easily download, install and manipulate gems on your system.
Q14. Explain the role of sub directory app/controllers and app/helpers.
Ans:
- 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.
Q15. What are helpers and how to use helpers in ROR?
Ans: Helpers ("view helpers") are modules that provide methods which are automatically usable in your view. They provide shortcuts to commonly used display code and a way for you to keep the programming out of your views. The purpose of a helper is to simplify the view. It's best if the view file (RHTML/RXML) is short and sweet, so you can see the structure of the output.
Q16. What is the basic difference between GET and POST method?
Ans: GET is basically for just getting (retrieving) the data, whereas POST may used to do multiple things, like storing or updating data, or ordering a product, or sending E-mail etc.
Q17. What do you understand by rails migration and what it can do?
Ans: 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.
Q18. Mention the role of rails controller.
Ans: 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.
Q19. How do the following methods differ: @my_string.strip and @my_string.strip! ?
Ans: The strip! method modifies the variable directly. Calling strip (without the !) returns a copy of the variable with the modifications, the original variable is not altered.
Q20. What is Bundler?
Ans: Bundler is a new concept introduced in Rails3, which helps to you manage your gems for the application. After specifying gems in your Gemfile, you need to do a bundle install. If the gem is available in the system, bundle will use that else it will pick up from the rubygems.org.
Q21. Mention the differences between the observers and callbacks in ruby on rails.
Ans: 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.
Q22. What's the difference in scope for these two variables: @name and @@name?
Ans: @name is an instance variable and @@name is a class variable.
Q23. Does Ruby support constructors? How are they declared?
Constructors are supported in Ruby. They are declared as the method initialize, shown below. The initialize method gets called automatically when Class.new is called.
Q24. What is the log that has to see to check for an error in ruby rails?
Rails will report errors from Apache in log/apache.log and errors from the Ruby code in log/development.log. If you're having a problem, do have a look at what these logs are saying. On Unix and Mac OS X you may run tail -f log/development.log in a separate terminal to monitor your application's execution.
Q25. What is the use of global variable $ in Ruby?
Ans: A class variable starts with an @@ sign which is immediately followed by upper or lower case letter. You can also put some name characters after the letters which stand to be a pure optional. A class variable can be shared among all the objects of a class. A single copy of a class variable exists for each and every given class. To write a global variable you start the variable with a $ sign which should be followed by a name character. Ruby defines a number of global variables which also include other punctuation characters such as $_ and $-k.
Q26. Where does the start_tabnav gets informations for tabs rendering in ruby rail?
Ans: The main Symbol let the start_tabnav method known to look for a special MainTabnav class where all are happens.