Questions tagged [ruby-on-rails-3]

Ruby on Rails is an open-source web development framework written in Ruby. Ruby on Rails follows the principle of convention over configuration, freeing you from having to re-invent things to stay productive. Use this tag only for Rails 3-specific questions, and also tag those questions [ruby-on-rails].

Ruby on Rails 3.0 was a major revision of Ruby on Rails in 2010, the open-source web development framework designed to make programmers happy and productive. Rails 3.0 brings a bunch of new features and improvements over older Rails versions, including:

  • Brand new router with an emphasis on RESTful declarations
  • New Active Record chainable query language built on top of relational algebra
  • Unobtrusive JavaScript helpers with drivers for Prototype, jQuery
  • Explicit dependency management with Bundler

See Ruby on Rails 3.0 Release Notes for more information.

Multiple sub-versions have been released, namely 3.0, 3.1 and 3.2. Questions regarding specific sub-versions of Ruby on Rails 3.0 can also be asked on the appropriate tags:

Resources:

See also:

56082 questions
207
votes
16 answers

Is it possible to run a single test in MiniTest?

I can run all tests in a single file with: rake test TEST=path/to/test_file.rb However, if I want to run just one test in that file, how would I do it? I'm looking for similar functionality to: rspec path/to/test_file.rb -l 25
Josiah Kiehl
  • 3,593
  • 3
  • 26
  • 28
207
votes
14 answers

ActiveRecord OR query

How do you do an OR query in Rails 3 ActiveRecord. All the examples I find just have AND queries. Edit: OR method is available since Rails 5. See ActiveRecord::QueryMethods
pho3nixf1re
  • 2,322
  • 2
  • 15
  • 13
206
votes
3 answers

Validate uniqueness of multiple columns

Is there a rails-way way to validate that an actual record is unique and not just a column? For example, a friendship model / table should not be able to have multiple identical records like: user_id: 10 | friend_id: 20 user_id: 10 | friend_id: 20
203
votes
3 answers

Rails: validate uniqueness of two columns (together)

I have a Release model with medium and country columns (among others). There should not be releases that share identical medium/country combinations. How would I write this as a rails validation?
203
votes
5 answers

What is the right way to override a setter method in Ruby on Rails?

I am using Ruby on Rails 3.2.2 and I would like to know if the following is a "proper"/"correct"/"sure" way to override a setter method for a my class attribute. attr_accessible :attribute_name def attribute_name=(value) ... # Some custom…
Backo
  • 18,291
  • 27
  • 103
  • 170
202
votes
11 answers

How do I fix the "You don't have write permissions into the /usr/bin directory" error when installing Rails?

I'm trying to install Rails 3 on a brand new MacBook Pro running OS X 10.6.3, Ruby 1.8.7, and Rails 2.3.5 and I'm wondering if I've hosed myself. So far, I've run these commands: $ gem update --system $ gem install arel tzinfo builder…
Jim
  • 13,430
  • 26
  • 104
  • 155
198
votes
3 answers

Ruby on Rails Callback, what is difference between :before_save and :before_create?

Could you explain in detail what the :before_save and :before_create Ruby on Rails callbacks are, and what they have to do with Rails validations? Does validation occur after :before_save or :before_create?
Agung Prasetyo
  • 4,353
  • 5
  • 29
  • 37
195
votes
15 answers

Repairing Postgresql after upgrading to OSX 10.7 Lion

I recently upgraded to OSX 10.7, at which point my rails installation completely borked when trying to connect to the psql server. When I do it from the command line using psql -U postgres it works totally fine, but when I try to run the rails…
Dave G
  • 4,376
  • 4
  • 25
  • 28
189
votes
9 answers

rake db:schema:load vs. migrations

Very simple question here - if migrations can get slow and cumbersome as an app gets more complex and if we have the much cleaner rake db:schema:load to call instead, why do migrations exist at all? If the answer to the above is that migrations are…
sscirrus
  • 55,407
  • 41
  • 135
  • 228
187
votes
5 answers

How to generate controller inside namespace in rails

I have namespace admin in controller, and I want to generate a controller inside of the admin folder. How can i do it with a Rails command?
user2088247
183
votes
12 answers

Equivalent of .try() for a hash to avoid "undefined method" errors on nil?

In Rails we can do the following in case a value doesn't exist to avoid an error: @myvar = @comment.try(:body) What is the equivalent when I'm digging deep into a hash and don't want to get an error? @myvar =…
sscirrus
  • 55,407
  • 41
  • 135
  • 228
181
votes
5 answers

rails simple_form - hidden field - create?

How can you have a hidden field with simple form? The following code: = simple_form_for @movie do |f| = f.hidden :title, "some value" = f.button :submit results in this error: undefined method `hidden' for…
177
votes
6 answers

Rails 3: I want to list all paths defined in my rails application

I want to list all defined helper path functions (that are created from routes) in my rails 3 application, if that is possible. Thanks,
wael34218
  • 4,860
  • 8
  • 44
  • 62
176
votes
14 answers

do..end vs curly braces for blocks in Ruby

I have a coworker who is actively trying to convince me that I should not use do..end and instead use curly braces for defining multiline blocks in Ruby. I'm firmly in the camp of only using curly braces for short one-liners and do..end for…
Blake Taylor
  • 9,217
  • 5
  • 38
  • 41
174
votes
7 answers

belongs_to through associations

Given the following associations, I need to reference the Question that a Choice is attached through from the Choice model. I have been attempting to use belongs_to :question, through: :answer to perform this action. class User has_many…
vinhboy
  • 8,542
  • 7
  • 34
  • 44