[英]Multiple tables in one migration in rails — how to name
rails, as I understand it, provides all sorts of benefits if you follow its naming conventions. 据我了解,如果遵循Rails的命名约定,它会提供各种好处。 Thus, in the Expect model I just created, I also got all the similarily named views and controllers, and in the migration
因此,在我刚刚创建的Expect模型中,我还获得了所有类似命名的视图和控制器,以及迁移
class CreateExpects < ActiveRecord::Migration
def self.up
create_table :expects do |t|
I note that it creates the table "expects" in this migration automatically, and I assume some benefits flow from that. 我注意到它会在此迁移过程中自动创建表“ expects”,并且我认为可以从中受益。 However, what if I want to create multiple tables in this migration
但是,如果我想在此迁移中创建多个表怎么办
For example, I am building a really simple app where students are asked two questions, 例如,我正在构建一个非常简单的应用,其中向学生询问两个问题,
The students will be asked two questions on a form
"Which courses do you think students will likely fail?" (Check all that apply)
Math, English, French, Science
"Which courses do you think students will likely pass?" (check all that apply)
Math, English, French, Science
but the suggestions from people for structuring the database is to create several tables, and since the tables will also have different names, how do I deal with the fact that I'm no longer conforming to the rails conventions 但是人们对结构化数据库的建议是创建多个表,并且由于这些表也将具有不同的名称,我该如何处理不再符合Rails约定的事实
For example, if the two questions are asked in one form which is in views/expects/new.html.erb (and _form), how do I make sure the data goes into the right table for each question? 例如,如果以views / expects / new.html.erb(和_form)中的一种形式询问两个问题,我如何确保数据进入每个问题的正确表中? and then pull it out all together in the othe rpage where I show the data?
然后在显示数据的其他页面上将其全部拉出?
I don't think I fully understand what you are asking, but I think I can help you just based on your problem. 我认为我不完全理解您的要求,但我认为我可以根据您的问题为您提供帮助。 This is what your database set up should look like.
这就是数据库设置的外观。
Create a table called student. 创建一个名为student的表。 You can use generate model command.
您可以使用generate model命令。
rails generate model student rails生成模型学生
Go in to your migration folder (db/migrate) and look for the student migration file. 进入您的迁移文件夹(db / migrate),然后查找学生迁移文件。
Add these lines in your migration file. 将这些行添加到您的迁移文件中。 Note that you could have used some association between student and subjects to set up the database, but for a simple app like this, I wouldn't bother.
请注意,您可能已经使用了学生和学科之间的某种关联来建立数据库,但是对于这样的简单应用程序,我不会打扰。
t.boolean :firstanswer_math t.boolean:firstanswer_math
t.boolean :firstanswer_english t.boolean:firstanswer_english
t.boolean :firstanswer_french t.boolean:firstanswer_french
t.boolean :firstanswer_science t.boolean:firstanswer_science
t.boolean :secondanswer_math t.boolean:secondanswer_math
t.boolean :secondanswer_english t.boolean:secondanswer_english
t.boolean :secondanswer_french t.boolean:secondanswer_french
t.boolean :secondanswer_science t.boolean:secondanswer_science
In the view, associate check box with each subject for each question. 在视图中,将复选框与每个问题的每个主题相关联。
FIRST QUESTION 第一个问题
<%= form.check_box :firstanswer_math => <%= form.check_box:firstanswer_math =>
<%= form.check_box :firstanswer_english => <%= form.check_box:firstanswer_english =>
... ...
Of course, you'd need appropriate methods in Controller file for student, but I'll omit that step. 当然,您需要在Controller文件中为学生提供适当的方法,但是我将省略该步骤。
Let me know if I adequately answered your question. 让我知道我是否充分回答了您的问题。
First Edit 第一次编辑
There isn't such flexibility. 没有这种灵活性。 You have to go in this order, object.attribute .
您必须按照此顺序object.attribute进行操作 。
Nope, you should be find without it. 不,您应该没有它。 You need has_many type of association when you want to associate two models with two different database tables.
要将两个模型与两个不同的数据库表关联时,需要has_many关联类型。 In our case, the app can be easily set up in one model and several attributes.
在我们的情况下,可以轻松地在一个模型和多个属性中设置该应用程序。 So having one table is perfectly fine.
因此,拥有一张桌子是完全可以的。
I think now I understand why you bring up "firstanswer_math.student". 我想我现在明白为什么您要提出“ firstanswer_math.student”。 Are you trying to find students with firstanswer_math set to true or something?
您是否要查找firstanswer_math设置为true或其他值的学生? If you want to do that you can use
find
method on student. 如果您想这样做,可以对学生使用
find
方法。 So for example, 例如
@students = Student.find_all_by_firstanswer_math( true )
I wouldn't say I'm a Ruby on Rails expert, but I think most Ruby on Rails developers would probably go for my implementation for your problem. 我不会说我是Ruby on Rails专家,但是我认为大多数Ruby on Rails开发人员都可能会为您的问题执行我的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.