Tag Archives: activerecord

[rails 4] add a reference to a table with another name

They default way in rails 4 to add foreign keys is simply add_reference :people, :user And this will add a column user_id to the people table, if the table exists. I have been looking in the rails code where this is handled, and it is really straightforward. Note that standard rails does not really do […]

automatically handle StaleObjectError in rails 3.x

There are different reasons why a ActiveRecord::StaleObjectError would occur. Sometimes it is caused because rails does not have a stable identity map yet. This means that if the same object would be retrieved via different associations, your rails process would not know they point to the same object, would keep different copies of the same […]

rewriting find_by_sql for rails 3

I am starting in Rails 3. I have a simple situation where i would select a parent based on a condition of one of its children. Normally i would write something like: Although this works, it is not the best solution to take. For one, it is vulnerable to sql-injection and secondly it shows a […]