Issue when retrieving records with empty array

In Rails 4 you can use User.where.not(id: []) which will give you the correct result. It produces: SELECT “users”.* FROM “users” WHERE (1 = 1) Unfortunately User.where(‘id NOT IN (?)’, []) should be equivalent but it is not. It still gives you the wrong result: SELECT “users”.* FROM “users” WHERE (id NOT IN (NULL)) References: … Read more

Merge two ActiveRecord arrays and order by created_at

You’re very close. Concatenating the arrays is done with the plus sign: materials = books + articles Sorting the combined array can be done by calling the sort_by method (mixed in from Enumerable) and passing in the attribute prefixed with &: materials.sort_by(&:created_at) This won’t be good performance-wise for large result sets. You might consider deriving … Read more

CodeIgniter: INSERT multiple records without cycle

Codeigniter active record has a function insert_batch i think that is what you need $data = array( array( ‘title’ => ‘My title’ , ‘name’ => ‘My Name’ , ‘date’ => ‘My date’ ), array( ‘title’ => ‘Another title’ , ‘name’ => ‘Another Name’ , ‘date’ => ‘Another date’ ) ); $this->db->insert_batch(‘mytable’, $data); // Produces: INSERT … Read more

How can I have ActiveRecord’s pluck also return the column name for rendering in json?

vee’s answer is good, but I have one caveat. select instantiates a User for every row in the result, but pluck does not. That doesn’t matter if you are only returning a few objects, but if you are returning large batches (50, 100, etc) you’ll pay a significant performance penalty. I ran into this problem, … Read more

Correct setting of database connection pool database.yml for single-threaded Rails applications

Quite late to the party here, but I ran out of database connections today in production. Like a lot of people, I use Sidekiq to perform asynchronous jobs like sending emails for example. It is important to note that Sidekiq runs as a multithread process. So, I don’t just have a single-threaded Rails application, therefore … Read more

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