Wow, thanks for that! I'm using thinking sphinx, but the same thing applies, right?
On May 15, 10:12 am, Matt Jones <[email protected]> wrote: > You can also use the :symbol interpolation support in conditions, to > get code that looks like this: > > def self.find_stories # add parameters as needed > conditions = ['1 = 1'] # default; find complains if conditions is > empty > conditions << 'title LIKE :search' if by_title > conditions << 'user LIKE :search' if by_user > Story.find(:all, :conditions => [conditions.join(' AND '), {:search > => "%#{keywords}%"}]) > end > > This simple example works for a single search term; extending it to > split keywords on spaces and combine terms with ' OR ' (or ' AND ', > depending on your interpretation of keywords) is fairly > straightforward. > > If you're doing more complicated things in your searches, you might > also want to try using named scopes instead. Something like: > > def self.find_stories > proxy = Story > proxy = proxy.search_by_title(keywords) if by_title > proxy = proxy.search_by_user(keywords) if by_user > proxy > end > > ...where the scopes are defined on Story thus: > > class Story < AR::Base > named_scope :search_by_title, lambda { |keywords| kws = > keywords.split(/ /); { :conditions => [kws.map { |k| 'title > LIKE ?' }.join(' AND '), *(kws.map { |k| "%#{k}%" })] } } > > end > > and so on for user, where you can now add options like :joins and/ > or :include if user is defined on another table. > > --Matt Jones > > On May 14, 3:28 pm, Mike C <[email protected]> wrote: > > > I have a Search Model which contains search data. It looks like this: > > > keywords:string > > by_user:boolean > > by_title:boolean > > ... > > > So basically what I want is, if one of the booleans is true it will > > search that particular index. So if by_user is true and by_title is > > true, it will search the database of stories by user and title. If > > only by_user is true, then it will only search by user. > > > So here's what I'm planning in the Search Model: > > > def stories > > find_stories > > end > > > def find_stories > > find(:all, :conditions => ??) > > end > > > I'm confused has to how to make the conditions since it will have to > > check which booleans are true and then append onto the conditions. How > > would I do this? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

