Has_many creation: through entries 2x times

I have models

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 

Therefore, when I try to connect questions with testing on it, create:

 >> 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"> 

Created 6 SQL queries and 6 records in test_questions. What for?

+4
source share
5 answers

I created a very simple example that processes your example:

 class Question < ActiveRecord::Base has_many :testing_questions end class Testing < ActiveRecord::Base has_many :testing_questions has_many :questions, :through => :testing_questions end class TestingQuestion < ActiveRecord::Base belongs_to :question belongs_to :testing end 

.. and then I can just do the following and no duplicate entries are created:

 Loading development environment (Rails 2.3.5) >> q1 = Question.new => #<Question id: nil, title: nil, ask: nil, created_at: nil, updated_at: nil> >> q1.title = "Dit is de eerste vraag" => "Dit is de eerste vraag" >> q2 = Question.new => #<Question id: nil, title: nil, ask: nil, created_at: nil, updated_at: nil> >> q2.title = "Dit is de tweede vraag" => "Dit is de tweede vraag" >> q1.save => true >> q2.save => true >> tt = Testing.new => #<Testing id: nil, name: nil, description: nil, action: nil, created_at: nil, updated_at: nil> >> tt.questions => [] >> tt.name = "Test1" => "Test1" >> tt.questions << q1 => [#<Question id: 1, title: "Dit is de eerste vraag", ask: nil, created_at: "2010-05-18 19:40:54", updated_at: "2010-05-18 19:40:54">] >> tt.questions << q2 => [#<Question id: 1, title: "Dit is de eerste vraag", ask: nil, created_at: "2010-05-18 19:40:54", updated_at: "2010-05-18 19:40:54">, #<Question id: 2, title: "Dit is de tweede vraag", ask: nil, created_at: "2010-05-18 19:40:59", updated_at: "2010-05-18 19:40:59">] >> tt.testing_questions => [] >> tt.save => true >> tt.testing_questions => [#<TestingQuestion id: 1, question_id: 1, testing_id: 1, extra_info: nil, created_at: "2010-05-18 19:41:43", updated_at: "2010-05-18 19:41:43">, #<TestingQuestion id: 2, question_id: 2, testing_id: 1, extra_info: nil, created_at: "2010-05-18 19:41:43", updated_at: "2010-05-18 19:41:43">] >> 

In fact, this is exactly the same as yours, except for TestingQuestion (which you did not show). Does it help?

+2
source

The first problem might be naming your join table

 testing_questions 

Rails expects the join table name to be a union of two table names in alphabetical order

 question_testings 
0
source

If you look at the structure of the table, this is the relationship has_and_belongs_to_many between tables, there is no need to use has_many through the parameter here, since your join table will not have a model (there are no additional columns except identifiers). There is no need to create a model for the join table.

So, I suggest changing your models as follows

 
class Question < ActiveRecord::Base
WEIGHTS = %w(medium hard easy) belongs_to :test
has_many :answers, :dependent => :destroy
has_and_belongs_to_many :question_testings end

class Testing < ActiveRecord::Base
belongs_to :student, :foreign_key => 'user_id' belongs_to :subtest has_and_belongs_to_many :question_testings end

and please change the name of your table as question_testings.

Please see this link for more information http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

0
source

Without looking at the source of Rails or trying, I suggest trying to either remove "has_many: testing_questions" from the Question class, or add has_many ...: through it. Only now Rails only has to do with the join table, but not with the β€œreal” purpose on this side.

The connection table name should not cause any problems.

0
source

Your questions array contains 3 elements. When you create a Testing instance and specify to create it with :questions => questions , it will add them to the questions relation, but since this relation happens through another, they must be added to another, as a result, inserted into them twice. Typically, you will have a different model representing the connection table, so you have 3 records inserted in the Questions table, and 3 in the TestingQuestions connection table (has_many: through).

It seems to me that this is a problem in how you defined your TestingQuestions model, which you did not specify. For example, does it point to the same table as the Questions model?

0
source

Source: https://habr.com/ru/post/1310065/


All Articles