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.
Questions tagged [find-by-sql]
53 questions
20
votes
3 answers
Is it possible to combine will_paginate with find_by_sql?
In my Rails application I want to use the will_paginate gem to paginate on my SQL query. Is that possible? I tried doing something like this but it didn't work:
@users = User.find_by_sql("
SELECT u.id, u.first_name, u.last_name,
CASE
…

Tam
- 11,872
- 19
- 69
- 119
16
votes
6 answers
Rails, how to sanitize SQL in find_by_sql
Is there a way to sanitize sql in rails method find_by_sql?
I've tried this solution:
Ruby on Rails: How to sanitize a string for SQL when not using find?
But it fails at
Model.execute_sql("Update users set active = 0 where id = 2")
It throws an…

nothing-special-here
- 11,230
- 13
- 64
- 94
9
votes
1 answer
find_by_sql with array format in Rails 3
good day guys!
I'm using find_by_sql() in rails 3 to fetch records as follows.
@list=Email.find_by_sql(["SELECT * FROM Emails WHERE sent_id=?",params[:id]])
How to modify the same statement if multiple parameter applies for same attribute, say for…

Unknown Coder
- 1,510
- 2
- 28
- 56
4
votes
3 answers
find_by_sql and pagination with kamanari
I have a model that connects to an external database and I query using the find_by_sql method like so:
External.find_by_sql("SELECT * from table")
However when I add the .page(params[:page]), the error "undefined method page for class array"…

chourobin
- 4,004
- 4
- 35
- 48
4
votes
2 answers
ActiveRecord Custom Query vs find_by_sql loading
I have a Custom Query that look like this
self.account.websites.find(:all,:joins => [:group_websites => {:group => :users}],:conditions=>["users.id =?",self])
where self is a User Object
I manage to generate the equivalent SQL for same
Here how…

Viren
- 5,812
- 6
- 45
- 98
4
votes
3 answers
Convert array to ActiveRecord::Relation
I am using find_by_sql for this query, and it iss returning an array.
I want to do something like Job.hongkong_jobs.where(status: true) but I can't because it is an array.
scope :hongkong_jobs, -> { find_by_sql "SELECT DISTINCT(jobs.*) FROM" +…

bilal
- 219
- 1
- 5
- 15
4
votes
1 answer
Ruby on Rails: Instantiate associated models with find_by_sql?
Apparently, include and select can't be used simultaneously on a Rails find query, and this has been repeatedly marked as wontfix:
http://dev.rubyonrails.org/ticket/7147
http://dev.rubyonrails.org/ticket/5371
This strikes me as very inconvenient,…

William Jones
- 18,089
- 17
- 63
- 98
3
votes
0 answers
rails find_by_sql LIKE escaping %
I am trying to execute a find_by_sql statement which involves a SELECT statement with a LIKE operator. For sake of brevity, let's assume the statement is "SQL * FROM posts where lower(name) LIKE '%hello%'"
Seems like rails throws an exception about…

ekynox
- 459
- 2
- 7
- 13
3
votes
2 answers
rails using sql variables in find_by_sql
I have this query:
SET @current_group = NULL;
SET @current_count = 0;
SELECT user_id, MIN( created_at ) as created_at, CASE WHEN @current_group = user_id THEN @current_count WHEN @current_group := user_id THEN @current_count := @current_count + 1…

Luca Romagnoli
- 12,145
- 30
- 95
- 157
3
votes
4 answers
Rails & MySQL - How do I use the LIKE operator with % in find_by_sql in rails?
How do I remove the quotation marks around a variable so that I can use the LIKE operator in find_by_sql in rails?
@entries1 = Entry.find_by_sql(["SELECT `entries`.name as name FROM `entries` where `entries`.name like '%?%'",@something])
will…

JMT
- 63
- 3
- 7
2
votes
1 answer
Ruby on Rails .find_by_sql quotes issue
I am having an issue using the date variable in the following MySQL statement. The date variable contains a string which represents a date.
personal = Event.find_by_sql ["SELECT *
FROM events
…

Will M
- 23
- 4
2
votes
1 answer
Ruby on Rails: Is it possible to :include the other leg of a circular join table?
I'm working on an application that models friendships between users.
class User
has_many :friendships
has_many :friends,
:through => :friendships,
:conditions => "status = #{Friendship::FULL}"
end
class Friendship
…

William Jones
- 18,089
- 17
- 63
- 98
1
vote
3 answers
find_by_sql not working on rails 3.1.3
I am writing a quite complex query in Ruby on Rails 3.1.3, and I am using find_by_sql.
But I noticed a very strange behaviour, even if I use find_by_sql with very simple queries.
Here is a simple example:
Let' say that I have two models and related…

user1079400
- 11
- 2
1
vote
1 answer
Rails and sql: rewriting find_by_sql
I have a Product and Creator table, I also have a table called CreatorProduct which joins the creator with the product. A product can have many creators and a creator can have many products. What I want to do is find the products that have creators,…

cdahllof
- 21
- 2
1
vote
0 answers
How to make a query which searches by myClass_id and by either teacher_id or by replacingTeachers' ids (to contain an id) with Spring JPA?
I have an entity Subjects:
public class Subject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne
private Teacher teacher;
@ManyToMany
private Set…

FireAndBlood
- 21
- 3