Rails Migrations: tried to change the type of column from string to integer

I quote the manual about ALTER TABLE: A USING clause must be provided if there is no implicit or assignment cast from old to new type. What you need is: ALTER TABLE listings ALTER longitude TYPE integer USING longitude::int; ALTER TABLE listings ALTER latitude TYPE integer USING latitude::int; Or shorter and faster (for big tables) … Read more

DEPRECATION WARNING: Dangerous query method: Random Record in ActiveRecord >= 5.2

If you want to continue using order by random() then just declare it safe by wrapping it in Arel.sql like the deprecation warning suggests: Model.order(Arel.sql(‘random()’)).first # PostgreSQL Model.order(Arel.sql(‘rand()’)).first # MySQL There are lots of ways of selecting a random row and they all have advantages and disadvantages but there are times when you absolutely must … Read more

How to programmatically list all controllers in Rails

In Rails 3.1+: Rails.application.routes This will give you all the controllers, actions and their routes if you have their paths in your routes.rb file. For example: routes= Rails.application.routes.routes.map do |route| {alias: route.name, path: route.path, controller: route.defaults[:controller], action: route.defaults[:action]} end Update: For Rails 3.2, Journal engine path changed, so the code becomes: routes= Rails.application.routes.routes.map do |route| … Read more

PostgreSQL: FATAL – Peer authentication failed for user (PG::ConnectionBad)

“Peer authentication” means that it’s using a unix socket and expecting the connecting unix user to have the same unix username as the postgresql username. Since your local unix username is funkdified and you’re trying to connect as user goodsounds over a unix domain socket (local) connection where your pg_hba.conf specifies peer authentication, Pg correctly … Read more

Getting a “bad interpreter” error when using brew

I got this error (much the same): /usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby: bad interpreter: No such file or directory /usr/local/bin/brew: line 26: /usr/local/Library/brew.rb: Undefined error: 0 and fixed by the solution below: Open brew.rb: $ sudo vim /usr/local/Library/brew.rb Change the first line’s 1.8 to Current: Before: #!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -W0 After: #!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby -W0 Then brew works for me. Hope … Read more

Can’t install pg gem on Windows

The message you’re getting is a clear indication that you lack something for the correct installation of that gem: Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. There is no Windows native version of latest release … Read more

Can nginx be used as a reverse proxy for a backend websocket server?

You can’t use nginx for this currently[it’s not true anymore], but I would suggest looking at HAProxy. I have used it for exactly this purpose. The trick is to set long timeouts so that the socket connections are not closed. Something like: timeout client 86400000 # In the frontend timeout server 86400000 # In the … Read more

Rails 4.0 Strong Parameters nested attributes with a key that points to a hash

My other answer was mostly wrong – new answer. in your params hash, :filename is not associated with another hash, it is associated with an ActiveDispatch::Http::UploadedFile object. Your last code line: def screenshot_params params.require(:screenshot).permit(:title, assets_attributes: :filename) is actually correct, however, the filename attribute is not being allowed since it is not one of the permitted … Read more