Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert custom sql query...

Replytotopic

custom sql query

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

Hello buddies,
anyone has an idea about a custom sql query which always returns me nil, despite this table is not empty….

ActiveRecord::Base.connection.execute(“select * from apppronet.cvdb_users”)

Actually, I’m trying to call a function, but I’ve seen that select from tables also returned me nil.

Thanks in advance.

 
120a

Authority 25
Posting Rating 0
Sign in to rate this post

Are you definitely getting nil back. With MySQL you should get a MySQL::Results object back that you can iterate over

results = ActiveRecord::Base.connection.execute("select * from customers")
while row = results.fetch_row do
    # process row here
end

Otherwise, have you tried creating a CvdbUser model and fetched the data with a find_by_sql method call?

This would also give you the benefit of returning ActiveRecord objects instead of just arrays of field values.

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

I tried this code in console:

ActiveRecord::Base.connection.execute(“select * from appPronet.cvdb_users”)
=> nil

And still don’t know why it is not doing as it should :) method connected? returns me true, not false…

Any others suggestions or experiences?

 
19mois

Authority 37
Posting Rating 82
Sign in to rate this post

Could you provide the database you use? MySQL? PostgreSQL?
Anyway, Tim is right and you should get a *SQL::Results object back.
Moreover, did you try the same query using CvdbUser class method, like following:

CvdbUser.find_by_sql("select * from cvdb_users")

If that works, maybe you don’t need to precise the database (i even didn’t know one can do that..)

ActiveRecord::Base.connection.execute("select * from cvdb_users")

Anyway, i guess you already defined your ‘apppronet’ database in your config/database.yml file.
And, if you’re in development mode, default rails behaviour is to find the “apppronet-development”

Wish it helps,
Guillaume

 
Me

Authority 62
Posting Rating 100
Sign in to rate this post

If you need pure values, ActiveRecord::Base.connection.select_values(“your SQL here”) might be your friend.

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

@Guillaume: I’m dealing with MSSQL DB.
Query with find_by_sql directly over model works fine, but don’t over AvtiveRecord::BASE…
I’ll have to think about the way I can call table-valued functions.

Thanks Clemens, that’s perfect way to call scalar-valued functions in rails.

thanks again!

 
Profile

Authority 0
Posting Rating 15
Sign in to rate this post

You can also do something like this (just an example):

results = YourController.find_by_sql(“SELECT xxx FROM table_controller WHERE field = 1”)

Replytotopic

Other Recent Topics

Ask a Rails expert : Couldn't find Product without an ID

Ask a Rails expert : HTML to XML ---> get title & description

Ask a Rails expert : Accessing controller actions from scripted page?

Ask a Rails expert : seledted option for select_tag

Ask a Rails expert : Merging fields from two tables into one, including duplicates

Ask a Rails expert : saving has_many :through

Ask a Rails expert : Use Rails to develop sites for both Designer and Programmer

Ask a Rails expert : Rails+RS232

Ask a Rails expert : Is this a good way to add Admin section

Ask a Rails expert : RSS feed maker in rails 2.1

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel