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
127
votes
4 answers

Rails: redirect_to with :error, but flash[:error] empty

I'm trying to do a redirect while setting the flash[:error] value. (Rails 3.0.10) In my view I have

<%= flash[:error] %>

<%= flash[:notice] %>

If I do a redirect_to show_path, :notice => "ok" it works fine, but…
Daniel
  • 1,515
  • 3
  • 10
  • 13
127
votes
5 answers

Build vs new in Rails 3

In the Rails 3 docs, the build method for associations is described as being the same as the new method, but with the automatic assignment of the foreign key. Straight from the docs: Firm#clients.build (similar to Client.new("firm_id" => id)) I've…
ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
127
votes
14 answers

Convert duration to hours:minutes:seconds (or similar) in Rails 3 or Ruby

I have a feeling there is a simple/built-in way to do this but I can't find it. I have a duration (in seconds) in an integer and I want to display it in a friendly format. e.g. 3600 would be displayed as "01:00:00" or "1 hour" or something. I can do…
cydonia
  • 1,273
  • 2
  • 9
  • 4
127
votes
11 answers

Find all records which have a count of an association greater than zero

I'm trying to do something that I thought it would be simple but it seems not to be. I have a project model that has many vacancies. class Project < ActiveRecord::Base has_many :vacancies, :dependent => :destroy end I want to get all the…
jphorta
  • 2,144
  • 3
  • 18
  • 27
126
votes
6 answers

Eager load polymorphic

Using Rails 3.2, what's wrong with this code? @reviews = @user.reviews.includes(:user, :reviewable) .where('reviewable_type = ? AND reviewable.shop_type = ?', 'Shop', 'cafe') It raises this error: Can not eagerly load the polymorphic association…
Victor
  • 13,010
  • 18
  • 83
  • 146
124
votes
8 answers

find() with nil when there are no records

In my current rails program when I use something like user = User.find(10) When there is no user with ID=10 , I will have exception like : ActiveRecord::RecordNotFound: Couldn't find User with ID=10 Can I get nil instead of raising exception so…
Eqbal
  • 1,819
  • 2
  • 16
  • 25
124
votes
16 answers

rails install pg - Can't find the 'libpq-fe.h header

$ sudo bundle install Result Fetching gem metadata from https://rubygems.org/........... Fetching gem metadata from https://rubygems.org/.. Using rake (0.9.2.2) Using i18n (0.6.1) Using multi_json (1.3.6) Using activesupport (3.2.8) Using…
Arthur Yakovlev
  • 8,933
  • 8
  • 32
  • 48
122
votes
2 answers

Index on multiple columns in Ruby on Rails

I'm implementing functionality to track which articles a user has read. create_table "article", :force => true do |t| t.string "title" t.text "content" end This is my migration so far: create_table :user_views do |t| t.integer…
Emil Ahlbäck
  • 6,085
  • 8
  • 39
  • 54
121
votes
5 answers

Rails 3.1: Engine vs. Mountable App

Can someone help me understand the differences between a Rails Engine and a Mountable app? In Rails 3.1, you can create either one with the "rails new plugin ___" command. rails plugin new forum --full # Engine rails plugin new forum…
121
votes
5 answers

How to display unique records from a has_many through relationship?

I'm wondering what is the best way to display unique records from a has_many, through relationship in Rails3. I have three models: class User < ActiveRecord::Base has_many :orders has_many :products, :through => :orders end class Products <…
Andy Harvey
  • 12,333
  • 17
  • 93
  • 185
120
votes
5 answers

How do I run a Ruby file in a Rails environment?

I want to run a Ruby file in the context of a Rails environment. rails runner almost does what I want to do, but I'd like to just give it the file name and arguments. I'm pretty sure this is possible since I've done it before. Can someone remind me…
weicool
  • 2,613
  • 5
  • 22
  • 19
120
votes
7 answers

How to add 10 days to current time in Rails

I tried doing something like Time.now + 5.days but that doesn't work, even though I vaguely remember seeing, and being very impressed, with being able to do something like 2.years etc. How do I do that in Rails 3?
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
119
votes
3 answers

WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

Possible Duplicate: What does “WARN Could not determine content-length of response body.” mean and how to I get rid of it? I just upgraded to rails 3.2.2, and now on rails s, page load, I get all these errors in the log: [2012-03-07 19:46:14]…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
117
votes
10 answers

Rails: How to change the text on the submit button in a Rails Form

i have listed my _form.html.erb file below what i would like to do is change the text on the submit button i know how to do it in html but not shure how to do it in Rails 3 %= form_for(@faq) do |f| %> <% if @faq.errors.any? %>
Rod Nelson
  • 3,301
  • 5
  • 22
  • 25
116
votes
11 answers

How to delete migration files in Rails 3

I would like to remove/delete a migration file. How would I go about doing that? I know there are similar questions on here but as an update, is there a better way than doing script/destroy? Also, should I do a db:reset or db:drop if I remove/delete…
alvincrespo
  • 9,224
  • 13
  • 46
  • 60