[Rails] Re: searching on model and related attributes
On Jun 16, 2:43 pm, Matt Harrison <li...@ruby-forum.com> wrote:
> Hi all,
>
> Haven't played with rails for a while so I'm a little stuck. I've got to
> make a search functionality across models.
>
> Let me try to explain this (sorry for lack of clarity, I've been reading
> so many search gems etc that I'm a little fuzzy now):
>
> * Stores have town and county fields
> * Stores has_many Products which each have their own name and level
>
> Users need to be able to find all Stores (possibily limited by town or
> county) which have one or more Products identified by name AND a minimum
> level.
>
> So I might search for Stores which have the Product 'Small Wooden Table'
> with a level of at least 4 AND have the Product 'Oak Table' at least
> level 10. Then I might restrict that to Stores in the county of
> 'Yorkshire'.
>
> The list of Products is pretty long (300+) and I don't think it would be
> user friendly to put them in a long drop-down list, so I'm tempted to
> let users type in the name and level manually. If they don't type the
> name correctly, then I guess they won't get the right results.
>
How big is your data. If you've only got a couple of hundred stores to
go with those products then something like
Select * from stores
Inner join products_stores on store_id = stores.id
Inner join products on products.id = product_id
Where products.name = 'small wooden table' and level > 4 and location
= "yorkshire"
Should work. You could loosen the condition on name to make it more
user friendly (in which case you might want to chuck a 'distinct' in)
or provide an autocomplete for the product name.
Beyond a certain point you might fid it better to go for a full blown
searchengine like elasticsearch which would make the product name
searching bit more powerful while still remaining fast with large
amounts of data
Fred
> I've looked at several gems (including searchlogic and ransack) but none
> seem to give me the ability to search related models in this way. I
> might have to knock together something from scratch but I don't want to
> end up being hideously inefficient.
>
> Grateful for any pointers on this, and please let me know if I haven't
> explained it well enough.
>
> Many thanks
>
> --
> Posted viahttp://www.ruby-forum.com/.
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home