开发者

Issues with Relationships

开发者 https://www.devze.com 2023-01-15 03:38 出处:网络
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

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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消