创建 has_many :through 记录 2x 次
我有模型
class Question < ActiveRecord::Base
WEIGHTS = %w(medium hard easy)
belongs_to :test
has_many :answers, :dependent => :destroy
has_many :testing_questions
end
class Testing < ActiveRecord::Base
belongs_to :student, :foreign_key => 'user_id'
belongs_to :subtest
has_many :testing_questions, :dependent => :destroy
has_many :questions, :through => :testing_questions
end
所以当我尝试将问题绑定到其创建的测试时:
>> questions = Question.all
...
>> questions.count
=> 3
>> testing = Testing.create(:user_id => 3, :subtest_id => 1, :questions => questions)
Testing Columns (0.9ms) SHOW FIELDS FROM `testings`
SQL (0.1ms) BEGIN
SQL (0.1ms) COMMIT
SQL (0.1ms) BEGIN
Testing Create (0.3ms) INSERT INTO `testings` (`created_at`, `updated_at`, `user_id`, `subtest_id`) VALUES('2010-05-18 00:53:05', '2010-05-18 00:53:05', 3, 1)
TestingQuestion Columns (0.9ms) SHOW FIELDS FROM `testing_questions`
TestingQuestion Create (0.3ms) INSERT INTO `testing_questions` (`question_id`, `created_at`, `updated_at`, `testing_id`) VALUES(1, '2010-05-18 00:53:05', '2010-05-18 00:53:05', 31)
TestingQuestion Create (0.4ms) INSERT INTO `testing_questions` (`question_id`, `created_at`, `updated_at`, `testing_id`) VALUES(2, '2010-05-18 00:53:05', '2010-05-18 00:53:05', 31)
TestingQuestion Create (0.3ms) INSERT INTO `testing_questions` (`question_id`, `created_at`, `updated_at`, `testing_id`) VALUES(3, '2010-05-18 00:53:05', '2010-05-18 00:53:05', 31)
TestingQuestion Create (0.3ms) INSERT INTO `testing_questions` (`question_id`, `created_at`, `updated_at`, `testing_id`) VALUES(1, '2010-05-18 00:53:05', '2010-05-18 00:53:05', 31)
TestingQuestion Create (0.3ms) INSERT INTO `testing_questions` (`question_id`, `created_at`, `updated_at`, `testing_id`) VALUES(2, '2010-05-18 00:53:05', '2010-05-18 00:53:05', 31)
TestingQuestion Create (0.3ms) INSERT INTO `testing_questions` (`question_id`, `created_at`, `updated_at`, `testing_id`) VALUES(3, '2010-05-18 00:53:05', '2010-05-18 00:53:05', 31)
SQL (90.2ms) COMMIT
=> #<Testing id: 31, subtest_id: 1, user_id: 3, created_at: "2010-05-18 00:53:05", updated_at: "2010-05-18 00:53:05">
在testing_questions中创建了6个SQL查询和6条记录。为什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我创建了一个非常简单的示例来处理您的示例:
..然后我可以执行以下操作,并且不会创建重复的记录:
实际上,这与您的完全相同,除了测试问题(您没有展示)。这有帮助吗?
I have created a very simple example that handles your example:
.. and then i can just do the following, and no duplicate records are created:
Actually that is completely the same as you have, except for the TestingQuestion (which you didn't show). Does that help?
第一个问题可能是联接表的命名
Rails 期望联接表的名称是按字母顺序排列的两个表名称的串联
A first issue could be the naming of your join table
Rails expects the join table’s name to be a concatenation of the two table names in alphabetical order
查看表结构,它是表之间的 has_and_belongs_to_many 关系,无需在此处使用 has_many 和 through 选项,因为您的连接表不会有模型(除了 id 之外不存在额外的列)。无需为连接表创建模型。
所以,我建议像这样改变你的模型
并将您的表名称更改为 Question_testings。
请查看此链接以获取更多信息http://guides.rubyonrails.org/association_basics.html#choosing- Between-has-many-through-and-has-and-belongs-to-many
Looking at the table structure it is a has_and_belongs_to_many relations between the tables, there is no need to use has_many and through option here as your join table will not be having a model( no extra columns other than ids are present). No need to create a model for join table.
So, I suggest to change your models like this
and please change your table name as question_testings.
Please have a look at this link for more info http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many
在不查看 Rails 源代码或尝试的情况下,我建议尝试从 Question 类中删除“has_many :testing_questions”或添加一个 has_many ... :through 。现在 Rails 只与连接表有关系,但与那一侧的“真实”目标没有关系。
连接表的名称不应在此处引起任何问题。
Without looking at the Rails source or trying around, I'd suggest to try either removing the "has_many :testing_questions" from the Question class or add a has_many ... :through there. Just now Rails just has a relation to the join table, but not to the "real" target from that side.
The name of the join table shouldn't cause any problems here.
您的
questions
数组包含 3 个项目。当您创建Testing
实例并指定使用:questions => 创建它时; questions
它将把它们添加到questions
关系中,但是因为这个关系是通过另一个关系实现的,所以必须先将它们添加到另一个关系中,导致它们被插入两次。通常,您会使用一些其他模型来表示连接表,以便将 3 条记录插入到 Questions 表中,并将 3 条记录插入到 TestQuestions 连接表中 (has_many :through)。在我看来,这似乎是您如何定义 TestQuestions 模型的问题,但您尚未展示这一点。例如,它是否指向与 Questions 模型相同的表?
Your
questions
array contains 3 items. When you create aTesting
instance and specify to create it with:questions => questions
it is going to add them to thequestions
relationship, but because this relationship is through another one, they must be added to the other one first, resulting in them being inserted twice. Typically, you would have some other model representing the join table, so that you have 3 records inserted into the Questions table and also 3 into the TestingQuestions join table (has_many :through).Looks to me like its an issue in how you have defined your TestingQuestions model, which you have not shown. For example, is it pointing to the same table as the Questions model?