简体   繁体   中英

Can I use limits, orders or date queries with the Ruby DBF gem?

I'm referring to this gem: https://github.com/infused/dbf

I've read the readme and scanned through the API documentation. It feels like I should be able to use ActiveRecord style queries on a DBF Table but it doesn't look like it.

I'm hoping to get the last X records, query by date or use order in some way to help with synching the DBF file to another database without going through all of it.

The only examples seem to be simple "finds" and I can't get any comparison or otherwise to work:

widgets.find :first, :slot_number => 's42'

Does anyone know how to do this? (another gem/technique is fair suggestion too).

While you cannot use ActiveRecord style queries, DBF::Table is an Enumerable , so you can utilize any of the enumerable methods to do most of what you want.

For example, let's say you only want widgets that were created between 1/1/2005 and 7/15/2005. Let's also assume that the widgets table has a created_date column. You could do something like:

range = (Date.new(2005,1,1)..Date.new(2005,7,15))
widgets.select { |w| range.includes?(w.created_date) }

Or to sort all widgets by their size column:

sorted_widgets = widgets.sort_by { |w| w.size }

If your DBF file is not too huge, you can also convert it to an array to get access to all of Ruby's Array methods. For example, let's get the last 10 records:

widgets.to_a.last(10)

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