tags:

views:

40

answers:

1

I am not clear yet on the proper way to run raw sql queries with sequel

currently I am trying this but not sure if it is the correct way

DB.fetch("SELECT * FROM zone WHERE dialcode = '#{@dialcode}' LIMIT 1") do |row|
 @zonename = row
end

What would be good is if I can run the queries as raw then access the results like normal

e.g if @zonename.name = "UK"

+1  A: 

I have a few pointers which may be useful ...

First, you could simply do

@zonename = DB.fetch("SELECT * FROM zone WHERE dialcode = '#{@dialcode}' LIMIT 1").first

NB: you are ignoring the fact that there could be more results matching the criteria. If you expect multiple possible rows to be returned then you probably want to build an array of results by doing ...

@zonename = DB.fetch("SELECT * FROM zone WHERE dialcode = '#{@dialcode}').all

... and processing all of them.

Second, the return set is a hash. If @zonename points to one of the records then you can do

@zonename[:column_name] 

to refer to a field called "column_name" - you can't do @zonename.colum_nname (you could actually decorate @zonename with helper methods using some meta-programming but let's ignore that for the moment)

Sequel is an excellent interface, the more you learn about it the more you'll like it.

Chris

Chris McCauley
thanks, I am able to progress now
veccy