0

I grab a list of results from the database. then I ran an if statement to delete some of the results, however I don't want to delete the objects from the database, just from the list of results I pulled.

so whats the alternative method to .delete (Array.delete(object) removes from database?)

def self.search(search)
    if search
        results = where('title LIKE ?', "%#{search}%")
        results.each do |event|
            if (((event.edate + event.minduration.minute) - Time.now)/60).round.to_i > 0.to_i #if the event is over? remove it from the list.
                results.delete(event)
            end
        end
        results
    else
        Event.all
    end
end

Or perhaps it would be best to just limit the search results to begin with, but I don't know how to combine these two conditions into one line:

results = where('title LIKE ?', "%#{search}%")
((event.edate - Time.now)/60).round.to_i > 0.to_i

Can someone help me with this problem

Community
  • 1
  • 1
MetaStack
  • 3,266
  • 4
  • 30
  • 67

1 Answers1

0

Please try to run it like this:-

results = where('title LIKE ? AND (event.edate - Time.now)/60).round.to_i > ?', "%#{search}%", 0.to_i)
Nikita Singh
  • 370
  • 3
  • 12
  • That gave an error about a bad number of arguments, I found this http://stackoverflow.com/questions/4224600/can-you-do-greater-than-comparison-on-a-date-in-a-rails-3-search which looked promising but the > Time.now doesn't actually work. Then I found http://stackoverflow.com/questions/17514539/math-with-datetime-in-sql-query-in-rails That gave an error about TIME_TO_SEC not being valid so maybe thats a version issue or something. – MetaStack Oct 02 '14 at 22:06
  • I forgot to put ? For second condition, could you please try now. – Nikita Singh Oct 03 '14 at 08:49
  • And also make sure that time.now format is same as that of event.edate – Nikita Singh Oct 03 '14 at 08:50
  • ActiveRecord::StatementInvalid in EventsController#index SQLite3::SQLException: near ".": syntax error: SELECT "events".* FROM "events" WHERE (title LIKE '%asdf%' AND (event.edate - Time.now)/60).round.to_i > 0) Extracted source (around line #17): 17 if !results.blank? – MetaStack Oct 03 '14 at 13:43