简体   繁体   中英

Query in Rails not returning any records

This is an extension of my previous question ( using a select, assign variable and retrieve records ).

I resolved the problem with passing the correct value to the controller, but my query still does NOT return any records, yet there ARE matching records in my database.

NEW edited controller for the app...

# GET /bentries/january
def monthlysumm
   @bentries = Bentry.by_month(params[:billDate])    
end

AND here is the addition to the model...

def self.by_month(mon)
   where("strftime('%m', billDate) + 0 = ?", mon)
end

This generates a SQL statement like this...

SELECT "bentries".* FROM "bentries" WHERE (strftime('%m", billDate) + 0 = '2')

If I go into SQLite Manager in Firefox and type (the equivalent) SQL statement of...

SELECT * FROM bentries WHERE (strftime('%m', billDate) + 0 = 2)

It returns the expected 3 records from the "bentries" table, but yet the code in app generates NO resulting recordset.

Can anyone see where I have gone wrong in the controller or model?!?

Your queries are not the same.

The one generated is looking for a string, the one you are typing in is looking for an integer.

Parameters come into the controller as strings so you need to make it an integer if necessary. For example:

def self.by_month(mon)
   where("strftime('%m', billDate) + 0 = ?", mon.to_i)
end

Or you can do it in the call to by_month , depending on whether you want the month in your by_month method to be an integer or a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM