加入模型创建测试失败,因为缺少相关模型,不知道为什么? [英] Join model creation test failing because it is missing related models, not sure why?

查看:21
本文介绍了加入模型创建测试失败,因为缺少相关模型,不知道为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是失败的测试:

before(:each) do
    @attr = { 
      :user_id => "1", 
      :project_id => "1",
      :owner_type => "client"
    }
  end

  it "should create a new instance given valid attributes" do
     Ownership.create!(@attr)
   end

这里是失败:

Failures:

  1) Ownership should create a new instance given valid attributes
     Failure/Error: Ownership.create!(@attr)
     ActiveRecord::RecordInvalid:
       Validation failed: User can't be blank, Project can't be blank
     # ./spec/models/ownership_spec.rb:14:in `block (2 levels) in <top (required)>'

我没有任何验证用户或项目不为空.这是我的所有权模型:

I don't have any validation for a User or Project to not be blank. Here is my ownership model:

class Ownership < ActiveRecord::Base
  attr_accessible :owner_type

  belongs_to :project
  belongs_to :user

  validates :user_id, :presence => true

  validates :project_id, :presence => true

  validates :owner_type, :presence => true

end

我错过了什么吗?我做错了什么?此外,所有权并未在实际应用程序中创建..这是我正在使用但不起作用的内容:

Am I missing something? What did I do wrong? Also, the ownerships aren't getting created in the actual app..this is what i'm using but isn't working:

current_user.ownerships.create(:owner_type => 'designer', :project => @project)

这是我的用户模型:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :admin, :projects

  has_many :ownerships
  has_many :projects, :through => :ownerships

  accepts_nested_attributes_for :projects

推荐答案

:user_id:project_id 添加到 attr_accesible.就像您为 :owner_type 所做的那样.所以它应该看起来像

Add :user_id and :project_id to attr_accesible. Like you have done it for :owner_type. So it should look like

attr_accessible :owner_type, :user_id, :project_id

这应该可以解决问题.

这篇关于加入模型创建测试失败,因为缺少相关模型,不知道为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆