Issues with Relationships - ruby-on-rails

You can take a look at the app I'm referring to at:
http://github.com/585connor/QA
So, I've built this question & answer app... kind of. I can get the
answers to be listed on their respective questions but I cannot figure
out how to get the user info to be displayed on those questions/answers.
For example I'd like to put the username next to each answer and the
username next to each question. Also, when viewing the show action of
the users controller, I'd like to be able to see a list of that
particular user's questions and answers.
There are three tables: questions, answers and users. Can you take a
look at the github repository and try to point me in the right direction
for what steps I should take/concepts I should learn in order to achieve
what I'm trying to do?

Becase you have a
belongs_to :user
in your question and answer model, you can access the associated user-model by calling .user on a question or answer object:
# controller
#question = Question.find :first
# view
<%= #question.user.name %>
Accessing the user's questions and answers is similar:
# controller
#user = User.find :first
# view
<% #user.questions.each do |question| %>
<%= question.title %>
<% end %>

Related

Creating a feed with acts_as_taggable_on in Rails

I'm trying to create a feed in Rails utilizing acts_as_taggable_on and I'm having a bit of trouble.
In my app there are Users who belong to Groups and every User selects several Tags for himself/herself using the acts_as_taggable_on context :user_tags. The Users create Posts which also have Tags, but of the context :post_tags.
What I'm trying to do is create a feed for the current User comprised of Posts from within his/her Group that have at least one :post_tag in common with the current User's :user_tags. So for example, if I have a User that chooses "Developer, Designer" as two :user_tags, I'd want to return all Posts from within his/her Group that have "Developer" and/or "Designer" as :post_tags.
After hours of fiddling around, here's what I tried in post.rb, but it gave me an error ("ERROR: column 'tag_id' does not exist LINE 1"), which seems odd given that tag_ids do exist for taggings.
# Returns posts with tags also attributed to the given user.
def self.from_posts_matching_tags_of(user)
matching_tag_ids = "SELECT tag_id FROM taggings
WHERE taggable_id = :user_id"
where("tag_id IN (#{matching_tag_ids}) OR user_id = :user_id", user_id: user)
end
Any suggestions for how to properly create such a feed method would be much appreciated. And if there's any other relevant code you want me to share, just let me know.
So, I figured out a solution that has worked thus far in my (admittedly limited) testing. Hope this is helpful to anyone else seeking to do something similar. Of course, if anybody knows a more efficient way to tackle this, I'm always open to improvements.
I added this to pages_controller.rb and it seems to do the trick:
def index
if logged_in?
#feed_items = Post.tagged_with([#current_user.user_tag_list], :on => :post_tags, :any => true)
end
end
And this on index.html.erb:
<% if #feed_items.any? %>
<% #feed_items.each do |post| %>
<%= link_to post.content, post %><br/>
<% end %>
<% end %>

rails quiz implementation: saving points per answer

I am making a quiz-like app that has multiple questions that have multiple answers. Each answer has a points and the id_of_next_question attributes - depending on the answer the user should be presented with a different question.
The easy way to do this would be to do sth like this:
< #question.answers.each do |answer| %>
<= link_to answer.content, question_path(answer.id_of_next_question) %>
< end %>
But this has the down side that the user would see the ids of the Questions in the path and I don't know how I would collect the answer.points with link_to.
What would be a proper way to make a i.e. .../play.html view that shows the 1st question, and depending on the answer, calculates points=points+answer.points and renders the next question with answers without changing the ".../play.html" path?
One way to achieve this would be to have your links act like a form which creates a new question_result (a model where you save the user's choice),
for example
< #question.answers.each do |answer| %>
<= link_to answer.content, question_results_path(:question_id => #question.id , :answer_id => answer.id) , :method => :post %>
< end %>
then in a question_results_controller, you can have a create action, which calculates the points using the answer_id, and redirects the user back to the play.html path, with as a parameter the next question id so that the user can continue the quiz. You could save the points total as a cookie (although a more proper way to do it would be to have a user model, and save the results per user in the db)

Rails .count action with specific :id

We have Questions and Answers, and every Question has_many :answers and those answers belongs_to :questions.
I want to display the CURRENT count of answers on the CURRENT Question.
i tried in my Answers_Controller :
def show
#question = Question.find(params[:question_id])
#answers_count = #question.answers.count
end
and then called on my view <%= #answers_count %>.
But i think i'm missing something here, because nothing is Displayed.
I found out that i don't need to call those in my Controller, i can instead just call
<%= #question.answers.count %>
Try changing:
#answers_count = Question.answers.count
to:
#answers_count = #question.answers.count
You are finding a particular question in your first line and assigning it to #question, but then are not querying that particular question (using Question instead of #question)
Also, look into the differences between count and size to make sure you are not querying the DB more than you have to -- this is a good discussion: ActiveRecord: size vs count

Rails Search Form

I'm creating an application that tracks users and achievements (think, xbox live, etc.) These tables are linked via a join table. I would like to have a search form on my index that lets users type in a users name and a new page is loaded with a list of all achievements that user has earned. I'm not entirely sure how to set up this search form, on the index, to actually search the user table and return the results on a new page. Any help would be greatly appreciated. If you require more information then I'll be happy to provide it.
Here's a bit of skeleton code to get you started based off what I think you need from what you have said. I hope this is useful.
For the search bit you could do something like this in your index view:
<%= form_for User.new, :url => "search" do |f| %>
<%= f.label :name %>
<%- f.text_field :name %>
<%- end %>
In your controller:
def search
q = params[:user][:name]
#users = User.find(:all, :conditions => ["name LIKE %?%",q])
end
and in your search view:
<%-#users.each do |user| %>
Name: <%=user.name %>
<%- user.achievements.each do |achievement| %>
<%= achievement.name %>
<%- end %>
<%- end %>
You would, of course, need to ensure the users and achievement models are correctly linked:
class User << ActiveRecord::Base
has_many :achievements
end
There are plenty of tutorials and things about this e.g.:
http://blog.devinterface.com/2010/05/how-to-model-a-custom-search-form-in-rails/
Look the thing is every basic explanation in Rails3 starting with the Initial Tutorial provided by them explains you how to setup a new Controller/Model. The example was only one of thousands explaining the same problem.
It is a very broad range of different things you can do to achieve this. Basically you have to put some code in the controller:
which handles the search (including the activerecord stuff or whichever technique you use to access your model)
which sets some variables necessary for the search form
Setup two routes etc... Its to broad and completely covered even by the basic official rails3 tutorial.
Here is an application based on searchlogic is very useful and you can search by whatever you want
https://github.com/railscasts/176-searchlogic
You may want to check out the Ransack gem. https://github.com/activerecord-hackery/ransack

Searching within an associated array in ruby - rails 3

So lets say we have a Post model, a User model and a View model.
When a user views a post, a new record is created in the Views table. The table links the user, the post, and the current time. Later if that user goes back to view the post again, the record is updated with a new time. Pretty basic stuff.
Posts has_many views, and Users has_many views
Views belongs to Posts and Users
In the index view of the Posts, I want to call the specific view for each post i.e.
<% #Posts.each do |post| %>
<%= post.name %><br/>
<%= post.views %> # This connects all of the views related to this post.
# How do I get the only one connected to this post and the current_user.id?
<% end %>
I feel like theres some simple way of accomplishing this that I'm totally forgetting
You could do something like
current_user.views.where(:post_id => post.id)
# this may work, not sure
current_user.views.where(:post => post)
or
post.views.where(:user_id => current_user.id)
# this may work, not sure
post.views.where(:user => current_user)

Resources