<a href="#" name="top">This list</a> of Frequently Asked Questions(FAQ) is divided into the following sections:

* "General":#general
* "Web Servers":#webservers
* "Associations":#associations
* "Performance":#performance
* "Other":#other
* "Database":#database

h2. General <a href="#general" name="general">#</a>

h4. When is Rails going to be released?

Rails is "currently available.":http://download.rubyonrails.com/ Hopefully v1.0 will be released in early or mid 2005.

h4. What is the license for Rails?

See [[License]].

h4. Why the name "Rails"?

R for Ruby, a play on Struts, and I liked the sound of it ;).

h4. How long has Rails been around?

Although not released until the 24th of July 2004, Rails has been in development since the Basecamp project started in mid/late 2003 (from http://www.loudthinking.com/arc/000446.html)

h4. Is there a Rails book in the works?

There are a few! See  the BookList.

h4. How big is the rails code base?

See [[RailsStatistics]]. 

p>. return to "top":#top


h2. Web Servers <a href="#webservers" name="webservers">#</a>

h4. What web-server can I use with Rails?

Rails supports [[mod_ruby]] (Apache), CGI, and FastCGI. The last two interfaces are available on a wealth of web-servers, but Apache will serve as the reference web-server and will be the one upon which all examples are based.

You may also want to consider using [[Lighttpd]].

h4. When I'm using [[WEBrick]] to run Rails, is there a way to handle multiple sites on the same port, like you can with Apache and <code>vhosts.conf</code>?

Currently, no. Do have a look at hacking @vendor/railties/webrick_server.rb@ if you want to give it a shot.

h4. Which is preferred, [[mod_ruby]] or FastCGI?

FastCGI is recommended for shared hosting environments because of [[mod_ruby]]'s shared interpreter limitations.

h4. Does Rails have something like PHP's @$_SERVER@ variable where you can access @$_SERVER['HTTP_USER_AGENT']@ and other details?

Rails has @@request.env@, which is quite similar. See "@@request.env@ in the API docs":http://api.rubyonrails.org/classes/ActionController/Base.html for more details (under the "Request" section)

h4. I'm trying out rails on Windows with Webrick.  I just created a controller called Interviews with @script/new_controller@.  However, when I try to open @http://localhost:3000/interviews@ I get a Webrick 404 error.  Is the URL rewriting not working in the webrick version?  If not, how should I do it instead?

Make sure that you put a trailing slash after your controller, like http://localhost:3000/interviews/

h4. I wish I didn't have to add the trailing slash onto @http://railsserver/somecontroller/@ to access the index of somecontroller. Is there a way around this?

This happens automatically with Rails 0.6.0 (not yet with WEBrick, though).

h4(#referer). How can I redirect the user back to the last action?

Use session or referer: @@session[:return_to] = @request.request_uri@ from sender, then @redirect_to @session[:return_to]@ on target. Or @redirect_to @request.env['HTTP_REFERER']@, referer is less reliable.

h4. Where do I put static files like images, stylesheets and javascripts?

Static files should go in the public folder. It is best to put images in public/images, stylsheets in public/stylesheets and javascripts in public/javascripts. This will allow you to use the built-in Helpers more effectively. In your rhtml, the url's would be relative to public. Thus it's  @/images/image.png@ *not* @/public/images/images.png@.

h4. Where do I put my applications images, stylesheets and javascripts, etc. There’s a directory called public, but I think I read something about not putting files directly in this location, but just links to them. Oh, and what is the URL for them?

Rails previously used symlinks. Now it's recommended that you just populate those real directories in public with your files. And then include the public directory in what you would check into CVS or Subversion.

p>. return to "top":#top


h2. Associations <a href="#associations" name="associations">#</a>

h4. What's the difference between <code>belongs_to</code> and <code>has_one</code>?

The difference has to do with which table has the foreign key. See an illustration of ActiveRecordAssociations. For more details see the paragraph titled "belongs_to or has_one":http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html from the API documentation.

h4. If I have an association set up, say authors have many posts, what's the correct way to create a new post from within the author?

<pre><code>@post = @author.post.build
@post.attributes = @params['post']
@post.save@</code></pre>

It will automatically fill the foreign key if it can. Of course @@author@ has to exist in the db at this point.

p>. return to "top":#top


h2. Performance  <a href="#performance" name="performance">#</a>

h4. How can I reduce the number of repetitive queries being used in my Rails app.

As of Rails 0.12 most repetitive queries can be avoided by making use of EagerLoadingOfAssociations. See also PiggyBackQuery for an explanation of how to use your own SQL where appropriate.

h4. What is the cost of Rails hitting the database to infer the properties of ActiveRecord objects? How often does this happen? Is this schema data ever cached? What other performance-related issues should I be aware of?

On CGI, everything reloads on every request. On FastCGI and [[mod_ruby]], AR will cache the column information. 

As of Rails 0.12 See benchmarking and profiling scripts have been available. See BenchmarkSuiteWish for more details.

p>. return to "top":#top


h2. Other  <a href="#other" name="other">#</a>

h4. Why do helpers have you pass in the string for the instance variable, instead of the actual object?

Because I still need to names of the variables to properly construct the form element, so you'd quickly end up with a lot of input looking like @text_field @post.title, "post", "title"@ -- instead of just saying it once.

h4. Is it possible to call a helper from the controller?

No, but you can move the helper method into the controller and add @helper_method :method_name@ after the definition to make it available to the view again.

h4. How can I measure the size of my rails application?

Rake will report a number of statistics on your codebase

<pre><code>$ rake stats</code></pre>

h4. How do I reverse a list in the view?

Given a situation where you have something like in your view:

<pre><code><% @posts.each do |post| %></code></pre>

The <code>#each</code> method can be replaced with "<code>#reverse_each</code>":http://www.ruby-doc.org/core/classes/Array.html#M000368 which will traverse the array in reverse order.

<pre><code><% @posts.reverse_each do |post| %></code></pre>

h4. How can I include an HTML class in a link using <code>link_to</code> ?

<pre><code>link_to "new", { :action => 'new' }, 'class' => 'inner'</code></pre>

See the "API Docs for <code>link_to</code>":http://rails.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000214 for more details.

h4. How do I prevent access to methods of a controller?

Make those methods private or protected.

h4. When should I use singular or plural names?

See WhatGetsPluralized.

h4. Q. Is there any solid examples or documentation on using observers?

I couldn't even get them working until I found this post:
http://www.loudthinking.com/arc/000362.html
and realized I needed to add something like observer :my_observer to the application.rb. Is that even the right place to put it??

h4. If plain old "id" being deprecated, why is it used in Rails?

For normal ruby, @id@ is depracated in favour of @object_id@. For \ActiveRecord, @id@ always does @read_attribute(primary_key)@.

h4. What statement is equivalent to @<%= ... %>@? render_text? printf?

p. @concat(string, binding)@ can be used if needed. See the API docs for #concat in "ActionView::Helpers::TextHelper":http://rails.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html

h4. Do Rails sessions automatically fallback to GET-based session ID propagation if users choose to disable cookies?

No. That doesn't mean it can't be done.

(I'm still just getting started with Rails, but I'm thinking maybe this would be something you could use a filter for.  The filter would scour the output for internal links and attach a session ID to the end of all of them.  This ID might need to appear after a question mark in the URL so that stupid search engines that didn't keep track of cookies wouldn't think they were scouring different pages.)

h4. How is @-%>@ different from @%>@ ?

The extra dash swallows the rest of the line (no newline in the output).

h4. How can I access the API docs offline? Are they available for download?

See OfflineDocumentation.

p>. return to "top":#top


h2. Database <a href="#database" name="database">#</a>

h4. What database can I use with Rails?

See [[DatabaseDrivers]].

h4. Problem: I have a field named the same as an AR class and its causing problems. What's up?

Don’t give your fields the same name as any of your Active Record classes or tables. For instance don’t use flavor in your candies table, if Flavor descends from Active Record::Base. Use flavor_id (or anything other than flavor) instead.

h4. How do I fix `No such file or directory - /tmp/mysql.sock` ?

See [[database.yml]].

h4. How should I store boolean values in the DB?

Use a @tinyint(1)@. See HowtoUseBooleanColumns for more details.

h4. Does ActiveRecord support @ENUM@ column types?

No, not currently.

h4. How do I fix @Packets out of order@ with \MySQL 4.1

See [[MySQL Database access problem]]

h4. What field names have special properties?

See MagicFieldNames.

h4. When using SQLite I get this error. What's wrong?

<pre><code>
ActiveRecord::ConnectionFailed in Finance#list
undefined method `show_datatypes=' for #<SQLite::Database:0xf6dcf8f0>
</code></pre>

As per directions in HowtoUseSQLite, make sure your using @sqlite-ruby@ and not @sqlite@ from the gems. For me that was '2. sqlite-ruby 2.2.3 (ruby)' on 'gem install sqlite'.

h4. I use PostgreSQL. When I make inserts inside a transaction, calls to @now()@ within SQL statements yield transaction-consistent timestampts (i.e. @now()@ always returns the timestamp when the transaction started). I can still do this with AR by making an explicit UPDATE after saving, but is there any way to set AR date fields to some magic object that will result in @now()@ being used in the INSERT statement generated by @ActiveRecord::Base.save@?

I'm not sure (I'm guess, having not looked into the code, so this is still an OpenQuestion), but MagicFieldNames may be your answer.

There was a "thread on the mailing list about this,":http://one.textdrive.com/pipermail/rails/2005-March/003863.html  but I was unable to see if the issue was resolved.

p>. return to "top":#top
