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
0
votes
4 answers
Activerecord find_by_sql does not output SQL query results
I am using activerecord and find_by_sql to output the results of the sql query:
S = Object.find_by_sql("SELECT * FROM foo")
S.each do |s|
puts "#{s}"
end
I get
#
#
etc...
I need the actual…

Mark
- 365
- 3
- 9
- 18
0
votes
0 answers
Calling JPA findBy method multiple times
I have a functionality where a list of A(Entity) objects is given which is being iterated to manipulate a functionality where in the iteration B(Entity)'s column value is fetched by C_ID and DATE multiple times(JPA method: findByCIdAndDate), there…

Thomson Ignesious
- 689
- 1
- 8
- 25
0
votes
1 answer
How to find a document with Bson type in mongo in c#
I want to find a role details with specified username in MongoDb with Drivers in C#.
I don't want to use any builders or linq methods.
I tried this to insert a Bson document and it worked.
var client = new…
0
votes
4 answers
spring boot repository: check if Date exists
In my reservation-entity i have a column "bookingDate" --> example: "2021-05-10 12:00:00".
So in this object the date and starttime of an user-booking gets displayed.
If a user wants to book a timeslot, i want to check first if the selected timeslot…

judo_ürgens
- 31
- 7
0
votes
1 answer
Rails - find_by_sql with heredoc raises error
I have this simple SQL in a heredoc
sql = <<-SQL
SELECT SUM(price) as total_price,
SUM(distance) as total_distance,
TO_CHAR(date, 'YYYY-MM') as month
FROM Rides
WHERE user_id = #{current_user.id}
GROUP_BY month
SQL
and a…

jedi
- 2,003
- 5
- 28
- 66
0
votes
2 answers
find_by function isn't successful eventhough it should
my programming team created an account activation function like in Michael Hartl's Rails tutorial. Now after a few weeks of coding the function stopped working. So that is the full function:
class AccountActivationsController <…

Mostah
- 25
- 7
0
votes
1 answer
ActiveRecord and getting unused records from a relationship, find_by_sql or named_scope?
Some context:
I have some models that look like this
Actor has_many Acts
Act belongs_to Actor, belongs_to Decision
Decision has_many Acts, belongs_to Prompt
Prompt has_many Decisions
What I need to do is in the ActorsController, get a random Prompt…

John
- 477
- 5
- 13
0
votes
0 answers
Active record find_by_sql, object field return nil in the first time
I run the following code in rails console:
functions = Function.find_by_sql("Select * from functions where active = 1 and role_id =1")
It returns:
[#]
When I get the…

Kelvin Tan
- 982
- 1
- 12
- 33
0
votes
1 answer
How to print findBySql yii2
im using findBySql in yii2, i need to print the count of the query, but i only got the 'cidade' index.
Query :
$a2m = AlarmesCorrentes::findBySql('SELECT COUNT(CIDADE) AS CONTA, CIDADE
from
alarmes_correntes WHERE lastUpdate >= DATE_SUB(NOW(),…

Kauê Zuquetto
- 79
- 6
0
votes
2 answers
Rails 4 - form object values accessed in the find_by_sql query
In rails 4 I try to access some of the form object values in the find_by_sql. Is it possible?
Part of the code (in the Active Admin class):
f.has_many :actions, allow_destroy: false, new_record: true do |ff|
ff.input :description,
…

Michal
- 139
- 3
- 14
0
votes
0 answers
Rails 4 - how to add for a scope with ".find_by_sql" another scope?
I use a scope like this:
# controller
sales_rep = Car.sales_rep(branch_id)
# model - scope
scope :sales_rep, -> (branch_id) {
find_by_sql("... a big, complicated query ...")
}
This works well so far, but I need to add another…

user984621
- 46,344
- 73
- 224
- 412
0
votes
2 answers
Converting find_by_sql syntax to proper Active Record AREL syntax in Rails 4
Given a System model with three-dimensional coordinates x, y, and z, I have written the following instance method to give me all of the other Systems within a set range of the specific System in question. Here's the code:
def systems_within(range =…

GreenPlastik
- 134
- 7
0
votes
1 answer
rails ActiveModel add attribure that is not persisted
I am trying to query model table:
> messages = Message.find_by_sql("select count(*) as messages_count,
> sender_id, recipient_id, min(is_read) as is_read from messages group
> by sender_id, recipient_id")
DB table contains fields sender_id,…
user4918987
0
votes
1 answer
Store data retrieved as result of find_by_sql method in ruby on rails
I am new to ruby on rails. I am trying to retrieve data from my database and make operation in my rails application. I am stuck while trying to use find_by_sql in my class Activity < ActiveRecord::Base
I have a rates table with structure as
id |…

Anil Pediredla
- 704
- 2
- 8
- 20
0
votes
1 answer
Rails Dynamic SQL with variables: undefined method find_by_SQL
I have a complex SQL statement that crosses several tables. I wish to pass variables into this statement dynamically and return the data in a JSON format. Ihave tested and know the query works in PHPMyAdmin and have placed it within the manSearch…

Sauron
- 6,399
- 14
- 71
- 136