Saving search results rails - ruby-on-rails

I'm doing a rather complex search on few of my models. I combine the results into one array which contains all of my models. I'm wondering what is the best way to get the permlink to my search.
I was thinking of creating another model named SearchResult which would have many to many relation to all my models in search described above.
So that way I can reference from single SearchResult all the models that are part of that search result? Is this a good approach to this? Should I do something else instead?

Theres a tutorial for this exact scenario on RailsCasts.
http://railscasts.com/episodes/111-advanced-search-form-revised

Related

ActiveRecord schema for multiple types of objects with common columns?

I'm working on a Rails 5 app for Guild Wars 2, and I'm trying to figure out a way to serialize and store all of the items in the game without duplicating code or table columns. The game has a public API to get the items from, documented
here: https://wiki.guildwars2.com/wiki/API:2/items
As you can see, all of the items share several pieces of data like ID, value, rarity, etc. but then also branch off into specific details based on their type.
I've been searching around for a solution, and I've found a few answers, but none that work for this specific situation.
Single Table Inheritance: There's way too much variance between items. STI would likely end up with a table over 100 columns wide, with most of them null.
Polymorphic Associations: Really doesn't seem to be the proper way to use these. I'm not trying to create a type of model that gets included multiple other places, I just want to extend the data of my "Item" model.
Multiple Table Inheritance: This looks to me like the perfect solution. It would do exactly what I'm wanting. Unfortunately, ActiveRecord does not support this, and all of the "workarounds" I've found seem hacky and weird.
Basically, what I'm wanting is a single "Item" model with the common columns, then a "details" attribute that will fetch the type-specific data from the relevant table.
What's the best way to create this schema?
One possible solution:
Use #serialize on the details (text) column
class Item
serialize :details, Hash
end
One huge downside is that this is very inefficient if you need to query on the details data. This essentially bypasses the native abstractions of the database.
I was in a similar situation recently. I solved by using Sequel instead of ActiveRecords.
You can find it here:
https://github.com/TalentBox/sequel-rails
And an implmentation example:
http://www.matchingnotes.com/class-table-inheritance-in-rails.html
Good luck

Categorization of models in Rails 4

The goal is to have a list of non-unique categories for the models to be sorted under. However, a model could be placed in multiple categories.
Aside from using an array attribute and searching through each array of each model (thus eliminating scalability), how would one categorize models? I am unable to find a proper association form as well, for that would clutter the database with many instances of the "same" category.
If you're using a newish version of postgresql and rails 4, you can build light-weight tagging very easily using postgres' array column types. Array columns can be indexed and there's a set of decent operations on tables containing arrays. Arrays are performant and it's pretty fun to build this functionality from scratch, especially if your requirements are pretty simply.
There's a couple "ifs" in there which might mean this isn't the approach for you. If you're curious, this blog describes the process pretty clearly: http://rny.io/rails/postgresql/2013/07/28/tagging-in-rails-4-using-postgresql-arrays.html.
Good luck!
Would something like acts_as_taggable_on work? Or do you need something more elaborate? Could you give an example of what you'd like the code to look like?

Returning Search Results in Rails

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

Sphinx search tricks

I have some questions regarding sphinx search and thinking sphinx gem
Is it possible to find similar results for record? For example - similar posts for current post in blog application?
If my app is indexing different models (for example in blog app - posts, comments and pages) is it possible to perform search on every model with one request, but not performing search in every model (Post.search 'string', Comment.search 'string' etc)?
Dont know about part 1 of your question, but i'm sure you can do the second i.e. Search across multiple models.
Here's the reference
http://freelancing-god.github.com/ts/en/searching.html#global

Best practices for multiple models in rails from - nested / non-nested, and validations

Note:
Posting this as a separate question as per Brian's comment (from how to handle multiple models in a rails form)
I'm trying to learn the best way to handle multiple models in a single rails form, when the models are both nested and non-nested. For the nested ones, I found these two tutorials to be helpful
http://weblog.rubyonrails.org/2009/1/26/nested-model-forms
http://railsforum.com/viewtopic.php?id=717
My questions are:
In the case of a non-nested model, how to handle multiple entries for the second model? Just run a loop, and use fields_for?
In both nested/non-nested cases, how to validate for duplicate values, when there are multiple entries for the second model? For example, if project is the primary and task is the secondary (child) model, and the user adds multiple tasks for the project, how to make sure there aren't duplicate tasks added, for that particular model?
One way would be to loop through the text values, and check for duplicates. Is there a better way to do it, at the object level, instead of string level?
You might want to take a look at this to handle multiple instances:
http://railscasts.com/episodes/73-complex-forms-part-1
Also, I think I've answered your second question here:
validating multiple models in a rails form
You should have a look at the Presenter Pattern, it helped me a lot!
You can start here:
http://blog.jayfields.com/2007/03/rails-presenter-pattern.html

Resources