You are here: Forums Ask a Rails expert custom sql query...
Posted in Forums : Ask a Rails expert
Authority 0
Posting Rating 0
Sign in to rate this post
|
Hello buddies, 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. |
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. |
Authority 0
Posting Rating 0
Sign in to rate this post
|
I tried this code in console:
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? |
Authority 37
Posting Rating 82
Sign in to rate this post
|
Could you provide the database you use? MySQL? PostgreSQL?
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. Wish it helps, |
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. |
Authority 0
Posting Rating 0
Sign in to rate this post
|
@Guillaume: I’m dealing with MSSQL DB. Thanks Clemens, that’s perfect way to call scalar-valued functions in rails. thanks again! |
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”) |
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