Questions tagged [find-by-sql]

Executes a custom SQL query against your database and returns all the results. The results will be returned as an array with columns requested encapsulated as attributes of the model you call this method from.

53 questions
0
votes
1 answer

How to make activerecord use a field generated on the fly by find_by_sql

I'm using find_by_sql with Activerecord which I generate another field there that doesn't in the original table as a combination of different fields like: select (field1 + field2) as new_field_name If I try to access the newly generated field…
Tam
  • 11,872
  • 19
  • 69
  • 119
0
votes
1 answer

Is it possible to define a single SQL query that draws a set of permissible foreign_keys from one table and then uses them to filter another?

Specifically, I'm trying to figure out if it's possible to generate SQL that does what I want to feed into Ruby-on-Rails' find_by_sql method. Imagine there are Users, who are joined cyclically to other Users by a join table Friendships. Each User…
William Jones
  • 18,089
  • 17
  • 63
  • 98
0
votes
1 answer

Rails 4, PostgreSQL: convert results of find_by_sql aggregate functions into OpenStruct

Working on a report/metrics page and I need to optimize the queries as much as I can so I am using find_by_sql for efficiency. One of my queries is doing some aggregate functions where I return a count and some sums. I'm assigning the results of…
Dan L
  • 4,319
  • 5
  • 41
  • 74
0
votes
1 answer

find_by_sql changes datatype of aliased columns

A basic ActiveRecord query returns an unquoted id, for example: User.first.attributes => {"id"=>2, ... In find_by_sql, the aliased columns are returned with values in quotes, see id in this case: all = User.find_by_sql("SELECT u.id as u_id from…
1192805
  • 988
  • 2
  • 10
  • 26
0
votes
1 answer

ruby on rails MySQL server version for the right syntax to use near '? find_by_sql

view: <% Favorite.find_by_sql("SELECT p.* FROM favorites as f LEFT JOIN posts as p ON p.user_id = f.user_id WHERE f.user_id = ? limit 15", @user.id).reverse.each do |post| %> i am getting the following error: Mysql2::Error: You have an error in…
stergosz
  • 5,754
  • 13
  • 62
  • 133
0
votes
1 answer

Rails find_by in a lower hierarchy level

I have the following models: Company has_many Employees has_many Projects Projects have a deadline:dateTime Now I want, with a statement that generates a single SQL, to get the next deadline from any project of a given company. i.e. def…
EasierSaidThanDone
  • 1,877
  • 4
  • 20
  • 29
0
votes
1 answer

missing attribute when using find_by_sql with RSPEC

I use a find_by_sql statement which works fine in development. When it comes to the test environment using spec, it outputs the error: ActiveModel::MissingAttributeError: missing attribute: registered I have run db:test:purge and db:test:prepare to…
Ralph Allen
  • 146
  • 1
  • 4
0
votes
2 answers

How to Sanitize the SQL in Rails?

I'm new to this RoR world, I've many SELECT sql queries in my RoR Application, something like this @replies = Offerreply.find_by_sql ("SELECT * FROM offerreplies WHERE offer_id="+params [:offer_id]) Some are very simple like above and some are…
Unknown Coder
  • 1,510
  • 2
  • 28
  • 56
1 2 3
4