Created
January 18, 2011 03:57
-
-
Save biagidp/783964 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Does self not refer to the target of the search?
nope, appears to be a reference to the Reservation class
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
What is this? I don't even.