2010-12-16 4 views
2

Учитывая следующие данные, команда не работает в Mongoid ... Я выхожу из командной строки, и она не устанавливается или не отображается. User.plan по-прежнему равен нулю. И я НЕ знаю, как это будет работать на веб-странице по сравнению с командной строкой. Раньше я работал с Rails, но по какой-то причине Mongoid ускользает от меня.Как создать ссылку в Mongoid?

План класса:

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 

выход Командная строка:

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 
=> {} 

ответ

0

Я думаю, вы должны использовать belongs_to и has_many (или HAS_ONE) вместо references_one и referenced_in. См. http://mongoid.org/docs/relations/referenced/1-n.html

+1

`reference_one` и` has_many` являются псевдонимами. Я сомневаюсь, что это что-то изменит. – rubish 2011-08-10 04:23:02

Смежные вопросы