During testing in Rails 3, updating a record causes related records to be deleted

StackOverflow https://stackoverflow.com/questions/22343170

  •  13-06-2023
  •  | 
  •  

سؤال

I'm running some tests using minitest in Rails 3. I have a requirements model and a ind_requirements model which serves as an intersection between the industries model and requirements model. I have a dependent :destroy set on the has_many on the requirements model, but removing that doesn't seem to have any impact on what I'm seeing.

This is all controller testing, so I'm not interacting with the UI or I would suspect something like what is reported here: Active record update_attribute executes a DELETE query on an associated object(child) of an object(parent) when parent is updated

When the @requirement.update_attributes is hit, then I get the following output in the log:

Processing by RequirementsController#update as HTML
Parameters: {"requirement"=>{"reqText"=>"second version", "reqTitle"=>"Second Version"}, "id"=>"1"}
User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
SQL (0.1ms)  SELECT "requirements"."id" AS t0_r0, "requirements"."reqTitle" AS t0_r1, "requirements"."reqText" AS t0_r2, "requirements"."created_at" AS t0_r3, "requirements"."updated_at" AS t0_r4, "requirements"."category_id" AS t0_r5, "requirements"."status" AS t0_r6, "requirements"."source_id" AS t0_r7, "requirements"."sortOrder" AS t0_r8, "requirements"."version" AS t0_r9, "requirements"."active" AS t0_r10, "categories"."id" AS t1_r0, "categories"."catName" AS t1_r1, "categories"."created_at" AS t1_r2, "categories"."updated_at" AS t1_r3, "categories"."catAbbr" AS t1_r4 FROM "requirements" LEFT OUTER JOIN "categories" ON "categories"."id" = "requirements"."category_id" WHERE "requirements"."active" = 't' AND "requirements"."id" = ? ORDER BY categories.catName LIMIT 1  [["id", "1"]]
 (0.2ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "role_assignments" ON "roles"."id" = "role_assignments"."role_id" WHERE "role_assignments"."user_id" = 1 AND (roleName = 'Editor')
 (0.2ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "role_assignments" ON "roles"."id" = "role_assignments"."role_id" WHERE "role_assignments"."user_id" = 1 AND (roleName = 'Administrator')
 (0.2ms)  SAVEPOINT active_record_1
 Industry Load (0.2ms)  SELECT "industries".* FROM "industries" INNER JOIN "ind_requirements" ON "industries"."id" = "ind_requirements"."industry_id" WHERE "ind_requirements"."requirement_id" = 1 ORDER BY indName
SQL (0.2ms)  DELETE FROM "ind_requirements" WHERE "ind_requirements"."requirement_id" = 1 AND "ind_requirements"."industry_id" = 2
SQL (0.5ms)  INSERT INTO "requirement_versions" ("category_id", "created_at", "reqText", "reqTitle", "req_id", "status", "updated_at", "version") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["category_id", nil], ["created_at", Wed, 12 Mar 2014 06:00:13 UTC +00:00], ["reqText", "This is a test general requirement for the purpose of, um, testing things"], ["reqTitle", "Test Requirement 19"], ["req_id", 1], ["status", "Public"], ["updated_at", Wed, 12 Mar 2014 06:00:13 UTC +00:00], ["version", 1]]
 (0.1ms)  UPDATE "requirements" SET "reqText" = 'second version', "reqTitle" = 'Second Version', "version" = 2, "updated_at" = '2014-03-12 06:00:13.418730' WHERE "requirements"."id" = 1
 (0.1ms)  RELEASE SAVEPOINT active_record_1

Note the DELETE FROM. This then breaks my test as once these records are deleted from the test database, my subsequent code doesn't work properly. This delete is not triggered when running in development, just in test.

Is there a config option I'm missing?

Here is the requirement.rb file (without all of the other methods that aren't part of this):

class Requirement < ActiveRecord::Base

has_many :ind_requirements, dependent: :destroy
has_many :requirement_versions
has_many :industries, through: :ind_requirements
has_many :responses
belongs_to :category
scope :active, where(:active => true)
default_scope active.includes(:category).order('categories.catName')
scope :submitted, where(:status => :Submitted)
scope :public, where(:status => :Public)

accepts_nested_attributes_for :requirement_versions

attr_accessible :reqText, :reqTitle, :industry_ids, :category_id, :catName, :status, :user_id, :source_id, :catAbbr, :reqNumber, :sortOrder, :requirement_versions_attributes

Here is the actual test case:

test "When a requirement is reverted, the previous associated industry list is restored" do
login_admin
@requirement = create(:requirement)

@industry = create(:industry, indName: "First Industry")
@indLink = create(:ind_requirement, requirement_id: @requirement.id)

@indLink.save!

put :update, id: @requirement, requirement: { reqText: "second version", reqTitle: "Second Version"}

@industry = create(:industry, indName: "Second Industry")

@indLink.industry_id = @industry.id

@indLink.save!

get :revert, id: @requirement

@requirement = Requirement.find(@requirement.id)

@indLink = @requirement.ind_requirements.first

assert_equal(@indLink.industry.indName, "First Industry")


 end
هل كانت مفيدة؟

المحلول

The problem is that when I was sending the update to the requirements, I was only sending the updated parameters: put :update, id: @requirement, requirement: { reqText: "second version", reqTitle: "Second Version"}, but not including the parameters for the checkboxes, as I wasn't modifying them. Unfortunately, the end result is that the industries are then deleted. I imagine because the absence of parameters is interpreted not that they aren't being updated, but rather that they have been deleted.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top