Skip to content

Instantly share code, notes, and snippets.

@biagidp
Created January 18, 2011 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save biagidp/783964 to your computer and use it in GitHub Desktop.
Save biagidp/783964 to your computer and use it in GitHub Desktop.
class Calendar < ActiveRecord::Base
has_many :reservations
end
class Reservation < ActiveRecord::Base
belongs_to :calendar
def self.search(search_fields)
results = self #this is where I'd like to access the collection search was called on
search_fields.each do |k, v|
results.send(k.to_sym, v)
end
return results
end
def self.search_email(email)
where("email like '%#{email}%'")
end
end
Questions:
1) Why does @calendar.reservations.search_email("test") work, but Reservation.all.search_email("test") does not. Both are arrays and that behavior seems inconsistant
2) When I call @calendar.reservations.search({:search_email => "test"}) what's the best way to maintain the scope of @calendar.reservations
@graywh
Copy link

graywh commented Jan 18, 2011

What is this? I don't even.

@biagidp
Copy link
Author

biagidp commented Jan 18, 2011

Working on search forms sans searchlogic. Takes params passed in from the form_tag form and calls methods on each one to narrow scope...or at least that's the idea

@graywh
Copy link

graywh commented Jan 18, 2011

Does self not refer to the target of the search?

@biagidp
Copy link
Author

biagidp commented Jan 18, 2011

nope, appears to be a reference to the Reservation class

@graywh
Copy link

graywh commented Jan 18, 2011

Reservation.search_email("test").all should work. The order of these things matters. Also, @calendar.reservations isn't pulled from the database until the data is needed, but calling "#all" pulls from the db immediately, so you can't use "#where" after it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment