我有这样的关系:
组织:
class Organization
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :email, type: String
has_and_belongs_to_many :users
end
用户:
class User
include Mongoid::Document
include Mongoid::Timestamps
include ActiveModel::SecurePassword
has_secure_password
#Fields
field :nick, type: String
field :name, type: String
field :email, type: String
field :password_digest, type: String #for bcrypt-ruby
#Validations
## Presence
validates :nick, presence: true
validates :email, presence: true
validates :password, presence: true, :on => :create
#Uniqueness
validates :nick, uniqueness: true
validates :email, uniqueness: true
#Relations
has_many :documents, dependent: :delete
has_many :storages, dependent: :delete
has_and_belongs_to_many :organizations
end
我可以保存组织和用户。该关系工作正常,但现在我想向组织添加一个所有者,它也是一个用户。我试过has_one :user, as: :owner
但没有用。