Subqueries in activerecord

Rails now does this by default 🙂 Message.where(user_id: Profile.select(“user_id”).where(gender: ‘m’)) will produce the following SQL SELECT “messages”.* FROM “messages” WHERE “messages”.”user_id” IN (SELECT user_id FROM “profiles” WHERE “profiles”.”gender” = ‘m’) (the version number that “now” refers to is most likely 3.2)

PostgreSQL -must appear in the GROUP BY clause or be used in an aggregate function

If user_id is the PRIMARY KEY then you need to upgrade PostgreSQL; newer versions will correctly handle grouping by the primary key. If user_id is neither unique nor the primary key for the ‘estates’ relation in question, then this query doesn’t make much sense, since PostgreSQL has no way to know which value to return … Read more

Using Rails 3.1 assets pipeline to conditionally use certain css

I’ve discovered a way to make it less rigid and future proof by still using the asset pipeline but having the stylesheets grouped. It’s not much simpler than your solution, but this solution allows you to automatically add new stylesheets without having to re-edit the whole structure again. What you want to do is use … Read more

Rails 3.1 asset pipeline: how to load controller-specific scripts?

To load only the necessary name_of_the_js_file.js file: remove the //=require_tree from application.js keep your js file (that you want to load when a specific page is loaded) in the asset pipeline add a helper in application_helper.rb def javascript(*files) content_for(:head) { javascript_include_tag(*files) } end yield into your layout: <%= yield(:head) %> add this in your view … Read more

Using Rails 3.1, where do you put your “page specific” JavaScript code?

The Asset Pipeline docs suggest how to do controller-specific JS: For example, if a ProjectsController is generated, there will be a new file at app/assets/javascripts/projects.js.coffee and another at app/assets/stylesheets/projects.css.scss. You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these … Read more