235
votes
18answers
137k views

Error installing mysql2: Failed to build gem native extension

I am having some problems when trying to install mysql2 gem for Rails. When I try to install it by running bundle install or gem install mysql2 it gives me the following error: Error installing ...
58
votes
10answers
26k views

Is Ruby pass by reference or by value?

@user.update_languages(params[:language][:language1], params[:language][:language2], params[:language][:language3]) lang_errors = @user.errors ...
104
votes
2answers
53k views

How is attr_accessible used in Rails 4?

In my model attr_accessible seems to no longer work within my Model. What is the way to allow assignment in Rails 4?
316
votes
7answers
81k views

A concise explanation of nil v. empty v. blank in Ruby on Rails

I find myself repeatedly looking for a clear definition of the differences of nil?, blank?, and empty? in Ruby on Rails. Here's the closest I've come: blank? objects are false, empty, or a ...
183
votes
5answers
76k views

Rails :include vs. :joins

This is more of a "why do things work this way" question rather than a "I don't know how to do this" question... So the gospel on pulling associated records that you know you're going to use is to ...
126
votes
5answers
55k views

Difference between attr_accessor and attr_accessible

In RoR, what is the difference between attr_accessor and attr_accessible. From my understanding, using attr_accessor is used to create getter and setter methods for that variable, so that we can ...
174
votes
11answers
125k views

How do I parse JSON with Ruby on Rails?

I'm looking for a simple way to parse JSON, extract a value and write it into a database in Rails. Specifically what I'm looking for, is a way to extract shortUrl from the JSON returned from the ...
88
votes
4answers
30k views

How to prevent browser page caching in Rails

Ubuntu -> Apache -> Phusion Passenger -> Rails 2.3 The main part of my site reacts to your clicks. So, if you click on a link, it will send you on to the destination, and instantly regenerate your ...
159
votes
9answers
32k views

How to find where a method is defined at runtime?

We recently had a problem where, after a series of commits had occurred, a backend process failed to run. Now, we were good little boys and girls and ran rake test after every check-in but, due to ...
60
votes
3answers
19k views

What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Can some one please describe the usage of the following characters which is used in ERB file: <% %> <%= %> <% -%> <%# %> what's the usage of each one ?
49
votes
7answers
22k views

ExecJS::RuntimeError on Windows trying to follow rubytutorial

UPDATE: Colin's suggestion of removing the line //= require_tree . has fixed the issue. I have wasted over 2 days trying to follow every suggestion out there and fix my issue. I am trying to follow ...
182
votes
7answers
84k views

Ruby on Rails: best method of handling currency / money

I'm new to Ruby on Rails and I'm working on a very basic shopping cart system. I have a table items that has a column price of type integer. I'm having trouble displaying the price value in my views ...
192
votes
6answers
60k views

What is the easiest way to duplicate an activerecord record?

I want to make a copy of an activerecord record, changing a single field in the process (in addition to the id). What is the simplest way to accomplish this? I realize I could create a new record, ...
41
votes
11answers
47k views

Rails model without database

I want to create a Rails (2.1 and 2.2) model with ActiveRecord validations, but without a database table. What is the most widely used approach? I've found some plugins that claim to offer this ...
53
votes
1answer
26k views

Unable to install gem - Failed to build gem native extension - cannot load such file — mkmf (LoadError)

Ruby 1.9.3 The part of Gemfile #............... gem "pony" gem "bcrypt-ruby", :require => "bcrypt" gem "nokogiri" #.................. When I'm trying to install gems, I get an error ...
140
votes
3answers
28k views

OO Design in Rails: Where to put stuff

I'm really enjoying Rails (even though I'm generally RESTless), and I enjoy Ruby being very OO. Still, the tendency to make huge ActiveRecord subclasses and huge controllers is quite natural (even if ...
100
votes
4answers
58k views

Rails params explained?

Could anyone explain params in Rails controller: where they come from, and what they are referencing? def create @vote = Vote.new(params[:vote]) item = params[:vote][:item_id] uid = ...
65
votes
18answers
63k views

Rails, MySQL and Snow Leopard

I upgraded to Snow Leopard using the disc we got at WWDC. Trying to run some of my rails apps now complains about sql (in /Users/coneybeare/Projects/Ambiance/ambiance-server) !!! The bundled ...
71
votes
21answers
45k views

How can I avoid running ActiveRecord callbacks?

I have some models that have after_save callbacks. Usually that's fine, but in some situations, like when creating development data, I want to save the models without having the callbacks run. Is ...
102
votes
5answers
24k views

In Ruby on Rails, how do I format a date with the “th” suffix, as in, “Sun Oct 5th”?

I want to display dates in the format: short day of week, short month, day of month without leading zero but including "th", "st", "nd", or "rd" suffix. For example, the day this question was asked ...
44
votes
11answers
87k views

no such file to load — rubygems (LoadError)

I recently installed rails in fedora 12. I'm new to linux as well. Everything works fine on Windows 7. But I'm facing lot of problems in linux. Help please! I've installed all the essentials to my ...
45
votes
5answers
23k views

Connecting Rails 3.1 with Multiple Databases

At ShowNearby we have been doing a very big migration to RoR 3.1 from PHP and we are facing several problems that may be some of you have solved before. We have big amounts of data and we decided to ...
67
votes
9answers
33k views

ruby-debug with Ruby 1.9.3?

I just updated to Ruby 1.9.3p0 and Rails 3.1.1. Now when I try to launch the server, it complains that I should install ruby-debug, even though it's already installed. % rails server ...
144
votes
2answers
66k views

Difference between rake db:migrate db:reset and db:schema:load

The difference between rake db:migrate and rake db:reset is pretty clear in my head. The thing which I don't understand is how rake db:schema:load different from the former two. Just to be sure that ...
74
votes
8answers
30k views

Best way to create custom config options for my Rails app?

I need to create one config option for my Rails application. It can be the same for all environments. I found that if I set it in environment.rb, it's available in my views, which is exactly what I ...
68
votes
15answers
22k views

Limitations in running Ruby/Rails on windows

In the installation documentation to RoR it mentions that there are many limitations to running Ruby on Rails on Windows, and in some cases, whole libraries do not work. How bad are these ...
67
votes
5answers
33k views

Rails sessions current practices

Anyone have any "best practices" tips for Rails and sessions? The default session type for Rails 3 is still CookieStore, right? I used SqlSessionStore for a while and it worked well, but I may move ...
25
votes
6answers
21k views

Getting the warning “Insecure world writable dir /home/chance ” in PATH, mode 040777 for rails and gem

I've tried this but it didn't work and seemed to be for osx. I have a fresh Ubuntu 10.10 install with rvm, rails 3 and ruby 1.9.2. I have a fresh rails app but using either gem or rails results in the ...
13
votes
2answers
7k views

Rails 4 turbo-link prevents jQuery scripts from working

I'm building a Rails 4 app and I have a few scattered js files that I'm trying to include "the rails way". I moved jquery plugins into the /vendor/assets/javascripts and I updated the manifest ...
7
votes
1answer
559 views

Ruby Block Syntax Error [duplicate]

Possible Duplicate: Ruby block and unparenthesized arguments I'm not sure I understand this syntax error. I'm using Carrierwave to manage some file uploads in a Rails app, and I seem to be ...
272
votes
8answers
60k views

What does “WARN Could not determine content-length of response body.” mean and how to I get rid of it?

Since upgrading to Rails 3.1 I'm seeing this warning message in my development log: WARN Could not determine content-length of response body. Set content-length of the response or set ...
130
votes
19answers
96k views

SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

I am using Authlogic-Connect for third party logins. After running appropriate migrations, Twitter/Google/yahoo logins seem to work fine but the facebook login throws exception: SSL_connect ...
36
votes
4answers
9k views

STI, one controller

I'm new to rails and I'm kind of stuck with this design problem, that might be easy to solve, but I don't get anywhere: I have two different kinds of advertisements: highlights and bargains. Both of ...
30
votes
3answers
12k views

Rails 3: alias_method_chain still used?

I was just reading about Gems/Plugin development for Rails 3 and ran across this post that says that alias_method_chain is no longer used. I can see the method is still there in ...
43
votes
6answers
35k views

How to extract URL parameters from a URL with Ruby or Rails?

I have some URLs, like http://www.example.com/something?param1=value1&param2=value2&param3=value3, and I would like to extract the parameters from these URLs and get them in a Hash. Obviously, ...
17
votes
8answers
2k views

Is there a clean way to avoid calling a method on nil in a nested params hash?

I'm interested in getting the nested 'name' parameter of a params hash. Calling something like params[:subject][:name] throws an error when params[:subject] is empty. To avoid this error I usually ...
15
votes
7answers
15k views

Altering the primary key in Rails to be a string

So I've got two models, State and Acquisition. State has_many Acquisitions. I felt like an autoincrementing integer primary key for 51 records was rather silly. So I altered the model for the State ...
8
votes
1answer
2k views

What does map(&:name) do in this Ruby code?

Trying to understand Ruby a bit better, I ran into this code surfing the Internet: require 'rubygems' require 'activeresource' ActiveResource::Base.logger = ...
39
votes
7answers
29k views

ruby/ruby on rails memory leak detection

I wrote a small web app using ruby on rails, its main purpose is to upload, store, and display results from xml(files can be up to several MB) files. After running for about 2 months I noticed that ...
91
votes
7answers
77k views

In Rails, how do you render JSON using a view?

Suppose you're in your users controller and you want to get a json response for a show request, it'd be nice if you could create a file in your views/users/ dir, named show.json and after your ...
48
votes
3answers
24k views

Rails has_many :through Find by Extra Attributes in Join Model

New to both Ruby and Rails but I'm book educated by now (which apparently means nothing, haha). I've got two models, Event and User joined through a table EventUser class User < ...
47
votes
8answers
26k views

rails + MySQL on OSX: Library not loaded: libmysqlclient.18.dylib

I'm just starting out with Ruby (and rails). I did the setup according to http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:ruby gems, using rvm. I have everything working well with ...
44
votes
7answers
18k views

What tools do you recommend to profile Rails apps? [closed]

I've been looking for profiling tools for Rails for a while. I'm currently playing and testing ruby-prof and railsbench, but I kinda frustrated with the amount of tweaking and mangling required to ...
69
votes
9answers
33k views

Rails 3: Get Random Record

So, I've found several examples for finding a random record in Rails 2 -- the preferred method seems to be: Thing.find :first, :offset => rand(Thing.count) Being something of a newbie I'm not ...
30
votes
5answers
13k views

Rails Model has_many with multiple foreign_keys

Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, ...
14
votes
4answers
6k views

How to create a deep copy of an object in Ruby?

I did some searching found some different methods and posts about creating a deep copy operator. Is there a quick and easy (built-in) way to deep copy objects in Ruby? The fields are not arrays or ...
3
votes
3answers
1k views

Ruby on Rails Best pratices - Big Controller vs Small Controller

I need some informations for the best pratices in Ruby on Rails, especially with Controler who have to do a lot of things, so, a simple "show" action is now up to lines. I know, it's not really good, ...
65
votes
4answers
63k views

Rails /lib modules and

I am writing a custom wrapper for open_flash_chart plugin. It's placed in /lib and load it as a module in ApplicationController. However, I have some Class hierarchy or smth problem. From any ...
27
votes
9answers
35k views

Best Ruby on Rails social networking framework [closed]

I'm planning on creating a social networking + MP3 lecture downloading / browsing / commenting / discovery website using Ruby on Rails. Partially for fun and also as a means to learn some Ruby on ...
67
votes
16answers
18k views

Get person's age in Ruby

I'd like to get a person's age from its birthday. now - birthday / 365 doesn't work, because some years have 366 days. I came up with the following code: now = Date.today year = now.year - ...