These questions are intended to query for intermediate level knowledge of the Rails framework.
Q) I have two tables, users and addresses. The users table has a foreign key column to the addresses table called address_id. This is a one user to a one address setup. How do I represent that relationship in the User ActiveRecord model?
A) Add the line: belongs_to(:address) to the User class.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) I have two tables, users and comments. The comments table has a foreign key to the users table. This is a one user to many comments setup. How do I represent that relationship in the Users model?
A) Add the line: has_many(:comments) to the User class.
Note: could have has_many(:comment) - singular - , but that doesn't make semantic sense for a has_many relationship.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) I have two tables, users and groups. Users can belong to multiple groups and groups can have multiple users. I've created a groups_users join table to hold the relationship. How do I represent that relationship in the Users model?
A) Add the line: has_and_belongs_to_many(:groups) to the Users table.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) Why was the table called groups_users and not users_groups?
A) Rails convention is to alphabetize the names of the tables that make up the join table. This allows Rails to find the correct table.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) If I wanted to write a method that was available to all of my views where would I put that method?
A) The method could be put in the RAILS_ROOT/app/helpers/application_helper.rb
Note: You could put the method in another file and then include the module into the application_helper.
Note 2: If you want a helper method only available to one view, put in the helper of the corresponding controller. If you have a users_controller, Rails expects a users_helper in the RAILS_ROOT/app/helpers directory. It won't throw an error if it isn't there, but will warn you.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) In a standard setup, if you wanted to call a method before every page request (controller action request), how would you do it?
A) Add a before_filter call in the ApplicationController to the method you want do call. So, if your method was "check_for_permission", it would look like:
before_filter(:check_for_permission)
Note 1: This is assuming all your other controllers are descendants of the ApplicationController, hence the "standard setup" condition on the question.
Note 2: You can put this on any controller, not just the ApplicationController.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) If I include other modules in a controller why should I make the methods in the module either protected or private?
A) If you don't make the methods protected or private they will be accessible via the url, unless you have a security layer that prevents that access.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) When creating a partial, what's the naming convention?
A) Partials start with an underscore: _my_partial.rhtml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) When calling render on a partial, how do you prevent a layout from being applied?
A) Pass :layout => false to the render call. It would look something like:
render(:partial => "my_partial", :layout => false)
Note: even though I use the name my_partial in my render call, Rails will look for a file named _my_partial.rhtml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) What is the base directory for plugins?
A) RAILS_ROOT/vendor/plugins
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) For a basic plugin, in order for files in the lib directory to actually be loaded by rails what do you need to do?
A) You need to "require" the files in the init.rb file. If you have a file called string_ext.rb in your plugin lib directory, your init.rb would look like:
require "string_ext"
Note: I will probably post some plugin basics, nothing super tricky.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I'm definitely going to have to categorize some other questions. Just too much to put under the RubyOnRails umbrella. So, yeah, there's more that could be added here. The first one will probably be a Migrations series of questions.
As usual, I'll add to this category of questions as I think of them.