How to display related post in rails application - ruby-on-rails

I'm a beginner for rails application, I have troubled some issue which is display related post, I already show post separately according to id I have not any idea how to show related post.
Below my code for show post separately according to id:
def postDetails
#details= Post.find(params[:post_id])
end
Now how can I rich this solution?

This is not an answer, so I'll delete if required.
--
Your question is highly ambiguous, meaning that it's open to interpretation in many different ways. Whilst not a problem, when it comes to application functionality, you need to be as specific as possible.
Contrary to the new buzzwords of "full stack engineer", "devops" etc - the core of computing is to design functions & algorithms which "compute" data. Your case is exactly the scenario where a professional developer would outline a spec, and work towards implementing it.
To answer your question as broadly as you asked it, you have to define how you wish to "relate" your posts.
There are several options (all involve data sampling) --
define each post by "tags" or similar categorization
create an algorithm to parse the title, pull out keywords & search for them
have users specifically define which posts are "related"
As you can see, there is one constant with the above - you need a "benchmark" to associate data. Be it keywords, tags, associated posts - you have to be able to identify the data you want.
Thus, you'll have to define the pattern you wish to employ. I can talk through each option; it would be too much to write without knowing how you want to do it.

Related

Search Gems for Rails

I was browsing reddit for the answer to this and came across this conversation which lists out a bunch of search gems for rails, which is cool. But what I wanted was something where I could:
Enter: OMG Happy Cats
It searches the whole database looking for anything that has OMG Happy Cats and returns me a an array of model objects that contain that value, that I can then use Active model serializer (Very important to be able to use this) on to return you a json object of search results so you can display what ever you want to the user.
So that json object, if this was a blog, would have a post object, maybe a category object and even a comment object.
Everything I have seen is very specific to one controller, one model. Which is nice an all but I am more of a "search for what you want, we will return you what you want, maybe grow smarter like this gem, searchkick which also has the ability to offer spelling suggestion.
I am building this with an API, so it would be limited to everything that belongs to a blog object (as to make it not so huge of a search), so it would search things like posts, tags, categories, comments and pages looking for your term, return a json object (as described) and boom done.
Any ideas?
You'll be best considering the underlying technology for this
--
Third Party
As far as I know (I'm not super experienced in this area), the best way to search an entire Rails database is to use a third party system to "index" the various elements of data you require, allowing you to search them as required.
Some examples of this include:
Sunspot / Solr
ElasticSearch
Essentially, having one of these "third party" search systems gives you the ability to index the various records you want in a separate database, which you can then search with your application.
--
Notes
There are several advantages to handling "search" with a third party stack.
Firstly, it takes the load off your main web server - which means it'll be more reliable & able to handle more traffic.
Secondly, it will ensure you're able to search all the data of your application, instead of tying into a particular model / data set
Thirdly, because many of these third party solutions index the content you're looking for, it will free up your database connectivity for your actual application, making it more efficient & scaleable
For PostgreSQL you should be able to use pg_search.
I've never used it myself but going by the documentation on GitHub, it should allow you to do:
documents = PgSearch.multisearch('OMG Happy Cats').to_a
objects = documents.map(&:searchable)
groups = objects.group_by{|o| o.class.name.pluralize.downcase}
json = Hash[groups.map{|k,v| [k,ActiveModel::ArraySerializer.new(v).as_json]}].as_json
puts json.to_json

Rails routing, deep nesting for external resources

I understand that it's a good practice to only include in the URL the parameters needed to determine the object of the model.
If I have 2 models, Post and Comment... a Post has many comments and a comment belongs to one post. A URL for a comment can be
/comment/:comment_id
and from associations I can determine which Post it belongs to but
Some rails apps need to access external resources(Via APIs for example). If the rails app needs to replicate a part of another external source, what is the right way to handle URLs and routing?
If for example a post has some comments, The URL for a comment can be
/post/:post_id/comment/:comment_id
or
/comment/:comment_id
The latter has one disadvantage which is that I can't determine which post it belongs to if the API of the external source doesn't determine that and this would cause some problems with navigation through the app but it's a short URL and allows the user to easily manipulate the URL to get another comment(which I see as an advantage). At the same time using the first(long) link would make the URL so long but I can know which post it belongs to.
The only solution I can think of is to make both possible but the user would never know that the short one exists if I make the long one the default. What do you think?
I always use the longer / spelled-out version myself. I don't mind that it's long and I can only see good things come from it as you're discovering here. I also think it's an advantage because then you can do things like this:
post = Post.find_by_id(params[:post_id])
comment = post.comments.find_by_id(params[:id])
The point being that you can't go "comment fishing" this way. You have to have the right post context in order to get at a specific comment. This may not matter a whole lot if comments aren't at all sensitive, but there are many thing in a web app that may be. So scoping finds by a root object (like post here) allows a quick permissions check that can be reused and without having to check on parent objects.
Anyway, that's my 2 cents. I never understood why people take offense to the longer urls. If they work for you then don't be afraid to use them!

Pass quiz score to Desire2Learn Valence via API

I am trying to integrate an external multimedia application with Valence. The application presents the user with quizzes along the way, with each question mapping to a learning objective. Using the API, how can I send the results of each quiz/objective to D2L so it's part of the overall course grade?
Thanks for your question, it's definitely a good one that I'd love to hear more about!
Right off the top, I'll say that unfortunately the short answer is that Valence currently doesn't support retrieving quiz information, nor accepting question responses to contribute to the user's quiz grade in the grade book, and competency-based evaluation. We recognize this is a big gaping hole in Valence, but the reason why we don't have it yet is due to an assessment tools re-architecture which we're embarking on now, which will put things in place to expose more assessments tools functionality via Valence.
That said, I'd love to capture more details about your use case so that we # D2L can be well-positioned to support it in the future.
Can I assume that you would want to perform the quiz create, update, delete operations, inclusive of learning objective question association, directly in D2L, and not via Valence?
Get Questions: Would you be looking to retrieve all quiz question information, providing a quiz identifier?
Submit Responses, and Trigger Evaluation: Would you be looking to submit all quiz question responses, in order to trigger automatic evaluation, to populate the quiz's score in the grade book, and contribute to competency-based evaluation? Would you ever have a case where a question response was provided which required manual evaluation (such as a short/long answer question type)? If so, how would you like to see that handled?
Many thanks again, I appreciate your time to communicate this request.

backbone.js and rails

I have a rails app where i have a Contact model and with an embedded address document so a contact can have multiple address's all stored in the one document.
I want to use backbone on the front end to have a contact page to edit contact details ie name, age and then also to add many address's.
What is the way to do this ? Should i be posting the whole contact model including all address's each time a field is changed or an address is added / removed ?
Or should i be doing pop / push atomic operations some how?
I am using mongomapper by the way.
I hope someone can explain the way this works?
thanks
rick
First, have a look at this question which discusses the full update versus the incremental update approach:
Voting system with Backbone.js
That is for a simple counter, so the $inc operator is not what you are looking for, however you can use other atomic operators (like $set), for in place updates, see here:
http://www.mongodb.org/display/DOCS/Atomic+Operations
The last thing you then will want to think about is the document size. If you allow the user to have an infinite number of addresses then be prepared to incur hits if/when the document exceeds its original size (plus some padding). That can mean more IO and updating indexes etc. A full discussion is beyond the scope of this question, but check out this page for a start:
http://www.mongodb.org/display/DOCS/Updating
Hope that helps!

RESTful route for a list of members that are not in a collection

I'm trying to figure out what the best way to show a list of members (users) that aren't a collection (group).
/users
is my route for listing all of the users in the account
/group/:id/members
is my route for listing all of the users in the group
/users?not_in_group=:id
is my current option for showing a list of users NOT in the group. Is there a more RESTFul way of displaying this?
/group/:id/non_members
seems sort of odd…
Either query parameters or paths can be used to get at the representation you want. But I'd follow Pete's advice and make sure your API is hypertext-driven. Not doing so introduces coupling between client and server that REST was intended to prevent.
The best answer to your question might depend on your application. For example, if your system is small enough, it may suffice to only support a representation consisting of a list of users and their respective groups (the resource found at /users). Then let the client sort out what they want to do with the information. If your system has lots of groups and lots of users, each of which belongs to only a couple of groups, your available_users representation for any group is likely to be only slightly smaller than the entire list of users anyway.
Creative design of media types can go a long way to solving problems like this.
Spoke with my partner. He suggested:
/group/:id/available_members
Seems much more positive.
The main precept of REST is "hypertext as the engine of application state". The form of the URI is irrelevant, what matters is that it is navigable from the representation returned at the application's entry point.

Resources