I’ve noticed something odd with Rails (4.1) ActiveRecord, where select and count sometimes mix badly: User.all.count => 103 User.all.size => 103 User.all.length => 103 So far, so good. I …
Tag: ruby-on-rails
Rails User.joins.not(…) in Active Record?
Im looking to query all Users without Comments in a single sql query? Models: class User < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :user ...
Rails ActiveRecord – how to fetch records between two dates
I have two date columns – from_date and to_date in a database table. Example: from_date: 2012-09-10 to_date: 2012-09-30 today: 2012-09-13 I need to fetch all records, if today’s date is between from_date and to_date. How do I do that with a SQL query? If I have loaded the respective record, I can easily decide, if today’s date is between from_date
rails scope and joins
I have tried everything i thought would work for this and am turning up nothing. in rails 3, I need to find all users with a cd player in their car. A car has one user and one radio, and a user belongs to a car, and a radio has many cars. I am stumbling on how I would perform
Rails order method; column named :order
I have a model Exercises and it has columns of :circuit and :order (among others). In a view, I am trying to order the Exercises first by :circuit and then by :order. When I use the following: @…
Rails Nested SQL Queries
I have a database model Position(lat,lon) which holds latitudes and longitudes. I have a controller action called show_close_by which recieves a position in degrees (my_lat, my_lon), a tolerance (in …
How to show SQL query log generated by a RSpec test?
I am writing a spec for my rails 3 application. I want to test that db transactions are really working. It would be really helpful to be able to see the sql queries being generated my app while being driven by the spec. Is there a way to see the queries just like in the rails console? I’m using Rails
Complex JOIN with ActiveRecord and Rails 3
I have the following models: I have to find all posts that belong to groups where user is a member. I have made it with this method: but it produces unefficient SQL: I want to make a query like this: How can I do this without using raw SQL? Answer something like this should work for you, although it requires
Default value not populating in migration with Rails and Postgresql
I am currently trying to run this migration: class AddDroppedProjectsCountToUser < ActiveRecord::Migration def self.up add_column :users, :dropped_projects, :integer, {:default=>0, :…