Displaying 20 results from an estimated 3000 matches similar to: "Using find_by_sql to get the sum of a column"
2005 Oct 17
8
Using active record for SELECT MAX(column) FROM ...
Hi,
Is there an easy way of querying an active record for a maximum column
value? I need to do queries like:
SELECT MAX(salary) FROM employees
TIA,
Jeroen
2005 Dec 30
9
SELECT MAX
How does one do a SELECT MAX query in rails? I''ve searched and searched and
tried lots of things with no luck.
I want to implement "SELECT MAX(column_name) from table_name
and have it return the maximum value from column_name. I just need to get
that one value, not the whole object...
Thanks for any ideas...
Shelby
_______________________________________________
Rails mailing
2006 Feb 25
5
MySQL client ran out of memory
I''m getting the following error unexpectedly for my rails app. I just
exported the databae from dreamhost and imported it on textdrive for
my rails app.
Mysql::Error: MySQL client ran out of memory: SELECT COUNT(*) FROM
comments WHERE (comments.listing_id = 2666)
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/abstract_adapter.rb:88:in
2005 Dec 30
3
Select SUM(?) Query
When using PHP, I was able to
enter a query against my list of invoices such as :
Select SUM(amount) from invoices where project="123"
This would give me the total amount of all invoices for project 123.
In rails, how would I do this? I tried using "find_by_sql," but
couldn''t figure out how to extract the results out of this. Thanks!
2006 Apr 20
1
Returning Count from find_by_sql
I want to return an int from a find_by_sql call. But when I use that
result to compare to another count the error tells me I have an array.
MODEL:
# Returns the number of comments in a specified post
def self.find_count(post_id)
find_by_sql(["SELECT COUNT(*) FROM commments WHERE post_id = ?",
post_id])
end
CONTROLLER:
def test
@post =
2006 Jul 16
3
pagination over custom-sorted collections?
I have a little web app where we need to paginate collections of
items. I''m trying to sort the collection and then pass it, sorted, to
a Paginator, and still be able to paginate through the collection,
across several pages, with the new sort order. The only thing I''m
doing differently from the regular scaffolding approach is this:
@order_pages = Paginator.new(self,
2008 May 30
9
find_by_sql without a model? how to do this?
Hi,
I''d like to do a "find_by_sql" without a model (e.g. <model
name>.find_by_sql("...")) as the results I get back are a once off special,
and I''m happy to handle them as an array. How do I do this?
"ActiveRecord::Base.find_by_sql(...)" does not seem to work?
Background - At the moment I''m doing within a model <model_name>,
2009 Apr 11
6
Inserting data from 1 table to another by using find_by_sql
Hi All,
I am inserting data from one table to another by using following method
@data=RoyaltyReportFiles.find_by_sql("insert into royalty_reports
(artist_name, album_name) select artist_name, album_name from
temp_royalty_reports where id=328417")
it saves the data into royalty_reports table but it gives following
error and application crashes. i am using rails 1.2.5
You have a nil
2006 Apr 13
7
Complex SQL in paginate command?
Hi all,
Is there a way to create pagination with a complex SQL, more complex
than the :conditions option will support?
I have two databases, houses and images. Houses has_many images, and
each Image belongs_to house.
I''m creating a search engine for the house records and I''d like to be
able to filter out all the houses without any associated images.
So far I''m
2006 May 15
8
set_table_name and self.table_name
I have some legacy tables that I used set_table_name on, I''m attempting
to write a method that will get key value from a sequence table and then
update it and return a value. I''m hoping to put this in the base
ActiveRecord method so I would like to reference the table name with
self.table_name or something...
class Contacts < ActiveRecord::Base
set_table_name
2006 Jan 03
4
Would someone like to tell me why this code will not solve my problem? (it''s short)
I am building an invoicing system but cannot use the auto_increment
field to determine the invoice number (because they are running 3
different companies off the one system. I need to find the last
invoice number from any given company and then add 1 to it to get the
next invoice number. BUT, there is a unique case on the very first
invoice produced because there is no earlier invoice
2006 May 23
8
ad-hoc query
How do you run an ad-hoc query that does not belong in any model? I
thought it was something like ActiveRecord::connection.find("my
query..."), but I can''t seem to find the documentation on it. I know
I''ve seen it somewhere before, though.
thanks,
Jeff
--
Posted via http://www.ruby-forum.com/.
2006 Sep 30
6
generating unique tracking numbers
Hello,
I''m interested in learning what folks in the Rails community do when
they need to
generate permanent unique numbers for tracking objects such as packing
slips, inventory items, customers, employees, etc.
It''s tempting to use the autogenerated id, but they''re not pretty
enough for human consumption and it can be problematic having assigned
ids if tables ever need
2006 May 02
4
ordering after a inject
Hi,
My results are getting out of order after I do the following command:
count = result.inject({}) { |hsh, row| hsh[row[''name'']] = row[''count''].to_i;
hsh }
Why?
Here are more specifics:
My complete method is this (based off of acts_as_taggable code - not'' DHHs,
but the original one):
def self.sql_to_count_plays(lookback)
sql =
2005 Oct 28
4
find_by_sql column types
Hello--
There must be a better way to do this. I have a class method in my model
that finds averages and does a few calculations using find_by_sql. The
problem I¹m encountering is that all computed columns from MySQL come back
as type string. E.g.,
def self.find_averages(domain_id)
if @@domain_average
return @@domain_average
else
@@domain_average =
2006 Jul 02
5
Question about setting field values for a belongs_to model
I have two tables, applicants, and skills where each applicant
"has_many" skills, and each skill "belongs_to" an applicant.
When I have a reference to an applicant and a (skill) id for one of
their skills, I thought to update their skills by using
@applicant.skills.find(id_of_skill).description = ''new description''
# even adding
2009 Sep 16
3
find_by_sql - multiple selects, same statement SQL error
I''m trying to get the following statement to work using variable
substitution (it works if hard-code the where (med_type) param in the
inner SQL statement):
med_type = ''game''
@mediaformats = MediaFormat.find_by_sql("select * from media_formats
where media_formats.media_type_id = (select media_type_id from
media_types where media_type = ?, :med_type) order by
2007 Jan 20
2
find_by_sql with named parameterized sql
I tried to use named parameters in my SQL query , using find_by_sql
Order.find_by_sql ([select * from orders where amount > ? and quantity >
?", [ @amount, @quantity ] works;..
but
Order.find_by_sql ([select * from orders where amount > :amount and
quantity > :quantity ", [ :amount => @amt, :quantity => @qty ] is not
working
a I wrong or should I use a plain select
2006 Oct 02
2
when to use find_by_sql
I have a bunch of queries to compute some counters, like
find_by_sql(<<-SQL
SELECT p.*, c.nfavourites
FROM people p, (
SELECT fp.person_id, COUNT(fp.user_id) as nfavourites
FROM favourite_people fp, users u
WHERE fp.user_id = u.id
AND u.cluster = ''#{in_cluster_of_user.cluster}''
GROUP BY fp.person_id
) as c
WHERE
2006 Apr 18
3
Join Queries? - find() or find_by_sql()
Hi All,
Is there a way to do join queries with find() or is it best to use
find_by_sql() instead?
I''m looking to do something like:
SELECT winery_name, wine_name FROM winery, wine
WHERE wine.winery_id = winery.winery_id
AND winery.winery_name LIKE ''Borg%'';
Thanks!
: )
Jason