Rails rake db:migrate has no effect

There’s a few reasons why your migrations won’t run, but the most common is that the system is already under the impression that all the migrations you’ve defined have already run. Each migration creates an entry in the schema_migrations table with the version column corresponding to the identifier number. If you want to force a … Read more

How can an ActiveRecord::Relation object call class methods

There’s not much documentation on the application on class methods to ActiveRecord::Relation objects, but we can understand this behavior by taking a look at how ActiveRecord scopes work. First, a Rails model scope will return an ActiveRecord::Relation object. From the docs: Class methods on your model are automatically available on scopes. Assuming the following setup: … Read more

How to translate active record model validations

The translation for the title would be: nl: activerecord: errors: template: header: one: “1 error prohibited this %{model} from being saved” other: “%{count} errors prohibited this %{model} from being saved” body: “There were problems with the following fields:” For translating the error messages, Rails will use the following order of translations: activerecord.errors.models.user.attributes.name.blank activerecord.errors.models.user.blank activerecord.errors.messages.blank errors.attributes.name.blank … Read more

How to eager load objects with a custom join in rails?

Can you not add the join conditions using ActiveRecord? For example, I have a quite complex query using several dependent records and it works fine by combining conditions and include directives Contractors.find( :all, :include => {:council_areas => :suburbs}, :conditions => [“suburbs.postcode = ?”, customer.postcode] ) Assuming that: Contractors have_many CouncilAreas CouncilAreas have_many Suburbs This join … Read more

Is there a “first_or_build” method on has_many associations?

I’m not sure if there is anything built into rails that will do exactly what you want, but you could mimic the first_or_initialize code with a more concise extension than you are currently using, which I believe does what you want, and wrap it into a reusable extension such as the following. This is written … Read more

Millisecond resolution of DateTime in Ruby

ActiveRecord should preserve the full precision from the database, you’re just not looking at it properly. Use strftime and the %N format to see the fractional seconds. For example, psql says this: => select created_at from models where id = 1; created_at —————————- 2012-02-07 07:36:20.949641 (1 row) and ActiveRecord says this: > Model.find(1).created_at.strftime(‘%Y-%m-%d %H:%M:%S.%N’) => … Read more

Filtering on activerecord relation without additional sql query?

The ActiveRecord:Relation only queries the db when its elements are accessed. So the sequence you have will not call the db at all unless you write something like u.first or tens.first. It is a bit different in Rails console as each statement results are printed to console so it executes the query each time. You … Read more

Getting typed results from ActiveRecord raw SQL

While I have no doubt that Björn Nilsson’s answer worked when he posted it, it is failing for me with Postgres 9.4 and PG gem version 0.18.2. I have found the following to work after looking through the PG gem documentation: pg = ActiveRecord::Base.connection @type_map ||= PG::BasicTypeMapForResults.new(pg.raw_connection) res = pg.execute(“SELECT ‘abc’::TEXT AS a, 123::INTEGER AS … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)