Passing parameters to raw SQL queries inside ruby on rails
In your model add this method
def self.execute_sql(*sql_array)
connection.execute(send(:sanitize_sql_array, sql_array))
end
This will let you sanitize and execute arbitrary SQL in an AR model
Then simply do this
ModelName.execute_sql("select address,phone,email,services from branches as b, workspaces as w
where b.workspace_id = w.id and w.name= ?", workspace_name)
In your model you can do this
sql_command = <<-SQL
SELECT address, phone, email, services
FROM branches as b, workspaces as w
WHERE b.workspace_id = w.id and w.name = :workspace_name
SQL
connection.execute(
sanitize_sql_for_assignment([sql_command, workspace_name: "whatever"])
)