依赖性破坏不起作用,has_many:model,:dependent => :破坏

时间:2014-05-07 20:30:05

标签: ruby-on-rails

在具有以下设置的Rails 2.3应用程序中,为什么在项目被销毁时我的依赖项目_endorsements不会被销毁?

项目模型

class Project < ActiveRecord::Base
  #...
  has_many :project_endorsements, :dependent => :destroy
  #...
end

项目代言模型

class ProjectEndorsement < ActiveRecord::Base
  belongs_to :project
end 

我的架构

create_table "projects", :force => true do |t|
  #...
  t.string   "data_type",                 :limit => 25
  #...
end

create_table "project_endorsements", :force => true do |t|
  t.integer  "project_id"
  t.integer  "contributor_id"
  t.text     "endorsement"
  t.integer  "sort_order",     :null => false
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "project_endorsements", ["project_id"], :name => "index_project_id"

我的测试失败

require 'spec_helper'

describe ProjectEndorsement do

  let(:project) {  Project.create!( data_type: "Book") }

  let(:contributor) {
    Contributor.new(
      first_name: "Anthony",
      last_name: "Swindle",
      display_name: "Anthony Swindle"
    )
  }

  let(:project_endorsement) {
    ProjectEndorsement.create!(
      project_id: project,
      contributor_id: contributor,
      endorsement: "endorsement",
      sort_order: 1
    )
  }

  context "when a project is deleted" do
    before do
      project.destroy
    end

    it "should remove the project endorsement as a dependency" do
      project_endorsement.should be_blank
    end
  end

end
$ spec spec/models/project_endorsements_spec.rb 
F

1)
'ProjectEndorsement when a project is deleted should remove the project endorsement as a dependency' FAILED
expected blank? to return true, got false
spec/models/project_endorsements_spec.rb:30:in `block (3 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p545@production/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p545@production/bin/ruby_executable_hooks:15:in `<main>'

Finished in 0.171014146 seconds

1 example, 1 failure

编辑:更新 - 这是一个控制台示例,显示项目无法识别has_many关联。这看起来很相关...也显示代言没有被删除,可能是因为has_many无法正常工作。

控制台示例

>> project = Project.create!(data_type: "Book");project.id
=> 11917
>> contributor = Contributor.first;contributor.id
=> 1
>> endorsement = ProjectEndorsement.create!(
?>       project_id: project,
?>       contributor_id: contributor,
?>       endorsement: "endorsement",
?>       sort_order: 1
>>     );endorsement.id
=> 6029
>> ProjectEndorsement.last.id
=> 6029
>> project.project_endorsements
NoMethodError: undefined method `project_endorsements' for #<Project:0x000000050b5040>
  from /usr/local/rvm/gems/ruby-1.9.3-p545@production/gems/activerecord-2.3.17/lib/active_record/attribute_methods.rb:275:in `method_missing'
  from (irb):10
  from /usr/local/rvm/rubies/ruby-1.9.3-p545/bin/irb:12:in `<main>'
>> project.destroy
=> #<Project id: 11917, data_type: "Book", created_at: "2014-05-08 13:42:22", updated_at: "2014-05-08 13:42:22">
>> ProjectEndorsement.last.id
=> 6029

2 个答案:

答案 0 :(得分:2)

好像你的

ProjectEndorsement.create!(
      project_id: project, # <-- Take a look here
      contributor_id: contributor,
      endorsement: "endorsement",
      sort_order: 1
    )

不属于project。 确保尝试检入控制台endorsement.project

你必须使用:

project_id: project.id,

project: project,

作为create!参数。

contributor

相同

答案 1 :(得分:0)

因为在之后项目被销毁之前它不会删除依赖对象,但是你的测试是在实际销毁命令之前触发的吗?

如果我离开基地,我会道歉 - 因为我还在学习如何进行适当的测试,但它会以这种方式向我发送......

其他可能性:

project_endorsements(复数)应为空白。但我希望它会返回错误,而不是错误......