我使用has_many:through将User模型和Project模型与Ownership模型相结合。我正在尝试设置Ownership属性的值,因为我使用accepts_nested_attributes_for在User和Project之间创建关联。
这是我的用户模型:
class User < ActiveRecord::Base
attr_accessible :name, :email, :admin, :projects
has_many :ownerships
has_many :projects, :through => :ownerships
accepts_nested_attributes_for :projects
这是我的所有权模式:
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
和我的项目模型:
class Project < ActiveRecord::Base
attr_accessible :name, :description
has_many :ownerships
has_many :users, :through => :ownerships
这就是我在创建所有权模型时尝试设置owner_type值的方法:
current_user.ownerships.create(:owner_type => 'designer', :project => @project)
由于某种原因,这不是创建所有权连接模型或(显然)设置所有权模型的owner_type值。我该怎么做才能解决这个问题?