Converting an array of objects to ActiveRecord::Relation

You can convert an array of objects arr to an ActiveRecord::Relation like this (assuming you know which class the objects are, which you probably do) MyModel.where(id: arr.map(&:id)) You have to use where though, it’s a useful tool which you shouldn’t be reluctant to use. And now you have a one-liner converting an array to a … Read more

Find rows with multiple duplicate fields with Active Record, Rails & Postgres

Tested & Working Version User.select(:first,:email).group(:first,:email).having(“count(*) > 1”) Also, this is a little unrelated but handy. If you want to see how times each combination was found, put .size at the end: User.select(:first,:email).group(:first,:email).having(“count(*) > 1”).size and you’ll get a result set back that looks like this: {[nil, nil]=>512, [“Joe”, “test@test.com”]=>23, [“Jim”, “email2@gmail.com”]=>36, [“John”, “email3@gmail.com”]=>21} Thought that … Read more

Eager load polymorphic

My guess is that your models look like this: class User < ActiveRecord::Base has_many :reviews end class Review < ActiveRecord::Base belongs_to :user belongs_to :reviewable, polymorphic: true end class Shop < ActiveRecord::Base has_many :reviews, as: :reviewable end You are unable to do that query for several reasons. ActiveRecord is unable to build the join without additional … Read more

Find all records which have a count of an association greater than zero

1) To get Projects with at least 1 vacancy: Project.joins(:vacancies).group(‘projects.id’) 2) To get Projects with more than 1 vacancy: Project.joins(:vacancies).group(‘projects.id’).having(‘count(project_id) > 1’) 3) Or, if Vacancy model sets counter cache: belongs_to :project, counter_cache: true then this will work, too: Project.where(‘vacancies_count > ?’, 1) Inflection rule for vacancy may need to be specified manually?

Can you do greater than comparison on a date in a Rails 3 search?

Note. where(:user_id => current_user.id, :notetype => p[:note_type]). where(“date > ?”, p[:date]). order(‘date ASC, created_at ASC’) or you can also convert everything into the SQL notation Note. where(“user_id = ? AND notetype = ? AND date > ?”, current_user.id, p[:note_type], p[:date]). order(‘date ASC, created_at ASC’)

How to list of all the tables defined for the database when using active record?

Call ActiveRecord::ConnectionAdapters::SchemaStatements#tables. This method is undocumented in the MySQL adapter, but is documented in the PostgreSQL adapter. SQLite/SQLite3 also has the method implemented, but undocumented. >> ActiveRecord::Base.connection.tables => [“accounts”, “assets”, …] See activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb:21, as well as the implementations here: activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:412 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:615 activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:176

Rails 3: Get Random Record

Thing.first(:order => “RANDOM()”) # For MySQL :order => “RAND()”, – thanx, @DanSingerman # Rails 3 Thing.order(“RANDOM()”).first or Thing.first(:offset => rand(Thing.count)) # Rails 3 Thing.offset(rand(Thing.count)).first Actually, in Rails 3 all examples will work. But using order RANDOM is quite slow for big tables but more sql-style UPD. You can use the following trick on an indexed … Read more

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