2010-12-16 10 views
2

以下のデータが与えられた場合、コマンドはモンゴイでは動作しません。コマンドラインを終了し、設定も表示もされません。 User.planはまだ等しくnilです。そして、私はそれがWebページとコマンドラインだけではうまくいかないことを知りません。私は以前にRailsと仕事をしてきましたが、何らかの理由でモンゴイドが私を逃しています。モンゴイドでreferences_oneを設定するにはどうすればよいですか?

計画クラス:

class Plan 
    include Mongoid::Document 
    field :active, :type => Boolean, :default => true 
    field :cost, :type => Integer 
    field :emails, :type => Integer 
    field :active_surveys, :type => Integer 
    field :name, :type => String 

    scope :active, where(:active => true) 

    scope :default, asc(:cost) 

    referenced_in :user 
end 

ユーザークラス:

class User 
    include Mongoid::Document 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, :lockable and :timeoutable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    field :name, :type => String 
    field :admin, :type => Boolean, :default => false 
    validates_presence_of :name, :email 
    validates_uniqueness_of :name, :email, :case_sensitive => false 
    attr_accessible :name, :email, :password, :password_confirmation, :remember_me 

    references_one :plan 

    delegate :emails, 
      :active_surveys, 
      :to => :plan, 
      :prefix => true 
end 

コマンドライン出力:私はあなたがbelongs_tohas_many(またはにhas_one)を使用すべきだと思う

ruby-1.9.2-p0 > p = Plan.where(:name => "Mega").first 
=> #<Plan _id: 4d09434aec286527fe000009, active: true, cost: 15, emails: 1000, active_surveys: 100, name: "Mega", user_id: nil> 
ruby-1.9.2-p0 > u = User.first 
=> #<User _id: 4d042734ec28651f2a000002, email: "[email protected]", encrypted_password: "-", password_salt: "-", remember_token: nil, remember_created_at: nil, reset_password_token: nil, sign_in_count: 1, current_sign_in_at: 2010-12-13 03:36:32 UTC, last_sign_in_at: 2010-12-13 03:36:32 UTC, current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: "First Last", admin: true> 
ruby-1.9.2-p0 > u.plan 
=> nil 
ruby-1.9.2-p0 > u.errors 
=> {} 
ruby-1.9.2-p0 > u.plan = p 
=> #<Plan _id: 4d09434aec286527fe000009, active: true, cost: 15, emails: 1000, active_surveys: 100, name: "Mega", user_id: BSON::ObjectId('4d042734ec28651f2a000002')> 
ruby-1.9.2-p0 > u.save 
=> true 
ruby-1.9.2-p0 > u.errors 
=> {} 

答えて

関連する問題