I have a problem with this scenario in ruby on rails 4, hope you guys can help me out.
I have products, brands, prices and users tables:
products: name, description,user_id,...
brands: title
prices: value, shipping_fee,...
users: username, password,...
I need a search page that allows people search products based on:
prices (minimum, maximum)
brands
seller name (it would be a join to users table)
What's your suggestion for the best way implementing such scenario? (or I should say cleanest way)
ps1: I need to validate fields in the search form.
ps2: I've seen sunspot and I don't think if that kind of full-text search be a good option for my scenario
Have a look at has_scope gem.
It allows you implement a search in controller using the resource scopes.
Related
Within my rails app I have two models. A User model and a Book model. A user uploads and has many books.
I'm trying to add 10 predefined categories to the user model, so that a user assigns as many of the 10 categories to themselves as they wish. For example, if i'm a user that uploads a several books, and the categories I chose in my user settings are "fiction" and "science"
If another user chooses "fiction", "science", "history" as their categories, I want to be able to show them all books uploaded by users that are under these three categories including all the books I uploaded since I am under the categories of "fiction" and "science"
What is the best way to implement this? I was thinking of using the acts-as-taggable-on gem but maybe this is too heavy weight and necessary. Should I just add a string column to my user model where each category is separated by a comma, so that I can split them up into an array and do queries with Active Record?
Obviously you could create your own solution for the problem but ActsAsTaggableOn is a very good solution for it. It is not such a heavy weight as other gems.
Bonus: You can go back any time if it becomes too heavy for your application. Which I doubt.
I have sunspot/solr set up to search products on my site. We need the ability to search users and another model (too much to explain what this is) in out app. Basically there is form for searching product via solr and this works well. There would be another form for searching users and the other form to search the other model.
I assume it is recommended to have a separate index for products, users, and the other model? It's seems best to keep the index from getting too bloated? Am I on the right track here?
All the models are indexed in the same index. And sunspot will also index the classnames into the index.
For my app searching is a big deal. I have many resources and each search result should pick bits from there and there.
Should I make a search controller?
What's the best architecture in that situation?
An exmaple use case:
The user searches for "Eos D5".
The app should reply with a field with the full product name, that's constructed out of manufacturers name and product model. Also if the product is available in neary (to user) shops then he is also told that there is a shop he can buy the product from near him. Manufacturer's name for product EOS D5 is "Canon".
That makes three models used: product, manufacturer, shop.
The output is something like
Canon EOS D5
For complex search functionality I'd recommend using a full-text search engine such as Solr.
There's a great gem called sunspot that makes integrating with Solr a breeze.
Let's say that each Product has a category. I want to ask the Users to select several categories that the user is interested in, and find the Products that have the same category. This is similar to what Quora, Stumbleupon, and Pinterest all do.
What would be the best way to set this database structure in Rails? Should I create 3 tables: User, Product, and Category, and make the relations
User has many Categories & Product has many Categories?
The problem I see with this is doesn't it create, rather than reference, a new instance of Categories to each row of Users and Products?
*extra: What if I wanted subcategories? For example, if the user chose Technology, it could further ask to choose between web dev, mobile dev, hardware, etc.
You could do that kind of 'recommendation' pretty easily.
Something like this should work (N.B.: I did not test this code, but it is right in spirit):
def recommended_products
joins(:categories, :products).where("product_id not in (?)", self.products)
end
Explanation of each bit:
joins(:categories, :products): this does a SQL join of users, products, and categories. This gives you a 'table' where each user-product-category combination is in it's own row.
.where("product_id not in (?)", self.products): adds a SQL where clause to filter out all the rows that have products in the current user's list of products.
The associations are not a problem. They don't create any new instances by themselves, only if you write code that creates new instances yourself.
As for sub categories, I think you'll do better to make that it's own question, as it's easily a whole post in itself.
I am having a problem implementing a special kind of search for my Rails application. I am working on an achievement system where you can search for a set of users in a search form (e.g., the query being "Ross, Adam, Jake") and it returns all of the common achievements that the users have unlocked (e.g., if users Ross, Adam, and Jake all had an achievement named "You are winner!"). I have three tables, one for achievements, one for users, and a join table. We have tested the associations and such, so we know that works.
My first idea was to put the search terms in an array and get the search results for each item in the array and place them into respective "search result arrays". Then, I was thinking to go through each item in search result array 1 to see if it appears in both of the other result arrays. The objects that appear in all three of the search result arrays would be returned and displayed on a page.
Is there an easy way to implement this without writing a bunch of my own code? Are there some functions I should know about? Any help will be appreciated!
Well, both Ransack and it's predecessor (MetaSearch) are useful gems for creating complex search forms.
In general I think you want to do something like select distinct achievement ids for user ids in an array. Off the top of my head I'm not quite sure how you should write it... others may know.
Look at the documentation on MetaSearch (more established) and see if you see a pattern that fits, if not check Ransack (more advanced).
You can use some autocomplete plugin for user names and convert the names to ids on the fly, that way you won't have to deal with converting user names to ids in backend later.
For common achievements, if a user can have a achievement only once, aggregating the results in join table and counting the results with achievement ids would be the way to go.
You can provide more details for a more detailed answer. :)
You can use Sunspot which is allows easy solr integration with Ruby and Rails