Tag Archives: Rails

Rails database.yml user vs username

In the database.yml file that Rails uses to define the database connection details for each environment you need to make sure it says username and not just user. If you use user than it will most likely work on your local machine, making you believe there is not issue.

BAD:

development:
  user: root
  password: root

 

GOOD:

development:
  username: root
  password: root

Rails test database not resetting correctly

I have no idea why, but so far I’ve had this problem twice. My specs suddenly start failing for no apparent reason. When digging deeper, it seems my test database is not reset before starting the tests. Therefor quite a few validations fail because of unique constraints. The following code cleans the test database up.

rake db:test:prepare

Select a single column with active record

I have an ‘Image’ model and I needed to select only the ‘id’ column as an array.

ids = Image.pluck(:id)

You can easily combine this with a where clause.

ids = Image.where(field: 'value').pluck(:id)

Check the official documentation.