在同一个数据库中创建多个记录:ID

编程入门 行业动态 更新时间:2024-10-24 09:24:48
本文介绍了在同一个数据库中创建多个记录:ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在我的Rails应用创建同一个记录的几个实例。我有测试和问题。 测试可以有多个问题。

I am trying to create several instances of the same record on my rails app. I have tests and questions. tests can have many questions.

我也有 testsessions 。我的 testsessions 表看起来是这样的:

I also have testsessions. My testsessions table looks like this:

id | user_id | test_id | question_id | answer

我试图让控制器创建相同testsession的几个实例,具有唯一不同的是在 question_id ,这将是从有问题拉同样为test_id 作为testsession。

I am trying to get the controller to create several instances of the same testsession, with the only thing differing being the question_id, which will be pulled from the questions that have the same test_id as the testsession.

目前,我有这个在我的控制器:

At the moment I have this in my controller:

def new @test = Test.find(params[:test_id]) @testsession = @test.testsessions.new @testsession.user_id = current_user.id @testsession.save @questions = @test.questions redirect_to action: :index end

但我不知道如何使它创建基于多个实例的 question_id 。

我有一个数组的 question_id 我要使用:

I have an array of the question_id that I want to use:

@questions = Question.where(test_id: @test.id).pluck(:id)

但我不知道如何把这个到控制器......最后我想表看起来是这样的:

But I don't know how to put this into the controller...in the end I would want the table to look something like this:

id | user_id | test_id | question_id | answer 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 1 | 1 | 1 | 3 | 1 | 1 | 1 | 4 |

答案永远是空的,因为这将是输入用户以后使用更新

这是如何做到这一点的任何想法将不胜AP preciated!

Any ideas on how to do this would be greatly appreciated!

推荐答案

我想我明白你想达到什么样的:你有一个测试的has_many的问题。当有人做了试验,要事先创建一个测试会话的每一个问题。

I think I understand what you're trying to achieve: you have a test that has_many questions. When someone does the test, you want to create a test session for every question in advance.

试试这个:

def new @test = Test.find(params[:test_id]) @test.questions.each do |question| @test.testsessions.create user: current_user, question: question end redirect_to action: :index end

作为一个侧面说明:在新控制器的动作是不是一个真正的好地方创造的东西,因为新是(通常)访问通过HTTP GET请求。

As a side note: the new controller action isn't really a good place to create things, because new is (normally) accessible through HTTP GET requests.

假设一下,如果你写的应用程序可以访问谷歌。如果谷歌发现了一个链接,这个控制器动作,它会参观,并意外地创建新的测试会话对象。可能很多次了。

Assume for a moment that the app you write is accessible to Google. If Google finds a link to this controller action, it will visit it and accidentally create new test session objects. Possibly many times.

这就是为什么创建或更改的东西在你的应用程序只应通过其他方式访问,就像一个POST控制器动作 - 搜索引擎和其他机器人不会碰那些

This is why controller actions that create or change something in your app should only be accessible through other methods, like a POST — search engines and other robots won't touch those.

即使您的应用程序不在公众开放,这是一个很好的习惯进入。

Even if your app is not accessible in public, it's a good habit to get into.

更多推荐

在同一个数据库中创建多个记录:ID

本文发布于:2023-10-30 04:06:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数据库中   在同一个   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!