如何在创建操作中将 2 个模型关联到另一个模型

发布于 2024-09-24 12:43:57 字数 254 浏览 8 评论 0原文

我有 3 个具有以下关联的模型。模型问题属于特定用户并有许多答案,答案模型既属于用户又属于问题。因此,用户模型有许多问题和答案。 我的答案模型具有 User_id 和 Question_id 字段,它们标识答案发布到的问题的所有者以及谁创建或拥有该问题。那么,如何将用户和问题 ID 分配给我的创建操作的答案,以及我的视图上用于发布答案的链接的结构是什么。另一个问题是,这是最好的方法吗?我的目标是在用户仪表板上显示用户的问题和答案,这样如果用户查看问题,他就可以看到该问题的答案,反之亦然。

I have 3 models with the following associations. A model Question belongs to a particular user and has many answers, an Answer model belongs to both a user and a question. A user model therefore has many questions and answers.
My answer model has the fields User_id and question_id which identifies the owner of the question the answer is posted to and who created or owns that question. So how do I assign the user and question ids to the answer on my create action and what is the structure of the link on my view for posting an answer. Another question is, is this the best way to go about it. My aim is to display the users questions and answers on the user dashboard such that if a user views a question he can see answers to that question and vice versa.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-10-01 12:43:57

简短回答:

Answer.create(params[:answer].merge(:question_id => @question.id, :user_id => @user.id)

@question.asnwers.create(params[:answer].merge(:user_id => @user.id)

@user.answers.create(params[:answer].merge(:question_id => @question.id)

长回答,我假设您可以获取登录用户,并且您的路由定义为:

map.resources :questions, :has_many => [:answers]

所以您的路由类似于:example.com/questions/:question_id/answers。然后你在控制器中执行 @question = Question.find(params[:question_id])

我不明白你所说的发布新答案的链接结构是什么意思。首先,您需要一个指向新内容的链接,例如 link_to('New answer', new_question_answer_path(@question))。

然后,在控制器中:

def new
  @question = Question.find(params[:question_id])
  @answer = Answer.new
end

在 new 视图中:

<% form_for :answer, [@question, @answer] do |f| %>
  Your form stuff
<% end %>

然后您将执行创建操作...我还没有尝试过这段代码,但您明白了,对吧?

Short answer:

Answer.create(params[:answer].merge(:question_id => @question.id, :user_id => @user.id)

or

@question.asnwers.create(params[:answer].merge(:user_id => @user.id)

or

@user.answers.create(params[:answer].merge(:question_id => @question.id)

Long answer, I'm assuming you can get the logged in user, and you have the routing defined as:

map.resources :questions, :has_many => [:answers]

So your routes are like: example.com/questions/:question_id/answers. And then you do in the controller @question = Question.find(params[:question_id])

I don't understand what you mean by structure of the link for posting a new answer. First you need a link to new, something like link_to('New answer', new_question_answer_path(@question)).

Then, in the controller:

def new
  @question = Question.find(params[:question_id])
  @answer = Answer.new
end

In the view for new:

<% form_for :answer, [@question, @answer] do |f| %>
  Your form stuff
<% end %>

And then you would have the create action... I haven't tried this code, but you get the idea, right?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文