Is there a more concise way to check if PGResult is empty?
ntuples
, a typo? It is faster and concise to use zero?
than == 0
if res.num_tuples.zero?
How to execute a raw update sql with dynamic binding in rails
It doesn't look like the Rails API exposes methods to do this generically. You could try accessing the underlying connection and using it's methods, e.g. for MySQL:
st = ActiveRecord::Base.connection.raw_connection.prepare("update table set f1=? where f2=? and f3=?")
st.execute(f1, f2, f3)
st.close
I'm not sure if there are other ramifications to doing this (connections left open, etc). I would trace the Rails code for a normal update to see what it's doing aside from the actual query.
Using prepared queries can save you a small amount of time in the database, but unless you're doing this a million times in a row, you'd probably be better off just building the update with normal Ruby substitution, e.g.
ActiveRecord::Base.connection.execute("update table set f1=#{ActiveRecord::Base.sanitize(f1)}")
or using ActiveRecord like the commenters said.
Rails Routing, how to put username before posts url?
In routes.rb
1 =>
get '/:username/posts/:id', to: "controller#action", as: my_custom_route
Here by you need to pass 2 dynamic param value with this path 1 -> username, 2-> post_title
my_custom_route_path(username: username_value, id: post_title)
2 =>
If you want just username as static in every routes for this action then
get '/username/posts/:id', to: "controller#action", as: :my_custom_route
Here you need to pass only one dynamic param that is post_title
my_custom_route(id: post_title)
Related Topics
Pow Not Loading Gem Properly While Rails S Works
How to Add "Access-Control-Allow-Origin" Headers to API Response in Ruby
Mongomapper Many to Many Issue with Array
Ruby Was-Sdk V2:Seahorse::Client::Networkingerror Exception: Ssl_Connect
Create and Initialize Instances of a Class with Sequential Names
Accessing Values Through Ajax Calls and Partials in Rails
Metasploit Induction of Bundle and Rake
Deleting a Line in a Text File
Ruby Selenium Web Drive: How to Find Specific Element by Xpath Div Id and CSS Class
Need Help on Join Table, Limiting Results to Only the Resource Id
Scope That Has Three Levels Deep Joins
Rexml::Document.New How to Give Encode Parameters on This Line
Require Self Made Gem in Jruby Fails After Update to Jruby-1.7.13
How to Properly Create a Associated Record Just After the Associator Record Creation
Multiple Limit Condition in Mongodb
How to Properly Install Bootsnap on Windows
Rails Console Is Not Outputting SQL Statements to My Development Log