Here is the test that is failing:
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
Here is the failure:
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)
here is my user model:
class User < ActiveRecord::Base
attr_accessible :name, :email, :admin, :projects
has_many :ownerships
has_many :projects, :through => :ownerships
accepts_nested_attributes_for :projects
Add
:user_idand:project_idtoattr_accesible. Like you have done it for:owner_type. So it should look likeThis should solve the problem.