2012-10-01 25 views
1

我有一個簡單的模型類獲得屬性:Mongoid - 無法設置/在某些情況下

class Sentence 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    field :content, :type => String 
    field :num_votes, :type => Integer 
    field :last_submitted, :type => Time 
    field :meaning_id , :type => String 
    belongs_to :word 
    belongs_to :user 
    attr_accessible :content,:num_votes,:last_submitted 
    attr_accessor :content,:num_votes,:last_submitted 
end 

我想設置內容屬性是這樣的:

sen = Sentence.first 
sen.content = "Hello" - This does not update the attribute(no error thrown) 
sen.write_attributes(content: "hello") - This does not update the attribute(no error thrown) 

但如果我這樣做

sen[:content] = "Hello" - This updates the attribute 
sen.write_attribute(:content,"Hello") - This updates the attribute 

我很困惑,這到底是怎麼回事,爲什麼在某些情況下,而在其他它does not我的更新工作。我也有get屬性的問題。 sen.content返回nil,但仙[:內容]返回正確的內容 我還有另外一個模型類,在這種情況下,上述所有四種方法獲取/設置屬性上的所有屬性的工作

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

    ## Database authenticatable 
    field :email,    :type => String, :default => "" 
    field :encrypted_password, :type => String, :default => "" 

    validates_presence_of :email 
    validates_presence_of :encrypted_password 

    ## Recoverable 
    field :reset_password_token, :type => String 
    field :reset_password_sent_at, :type => Time 

    ## Rememberable 
    field :remember_created_at, :type => Time 

    ## Trackable 
    field :sign_in_count,  :type => Integer, :default => 0 
    field :current_sign_in_at, :type => Time 
    field :last_sign_in_at, :type => Time 
    field :current_sign_in_ip, :type => String 
    field :last_sign_in_ip, :type => String 

    ## Confirmable 
    # field :confirmation_token, :type => String 
    # field :confirmed_at,   :type => Time 
    # field :confirmation_sent_at, :type => Time 
    # field :unconfirmed_email, :type => String # Only if using reconfirmable 

    ## Lockable 
    # field :failed_attempts, :type => Integer, :default => 0 # Only if lock strategy is :failed_attempts 
    # field :unlock_token, :type => String # Only if unlock strategy is :email or :both 
    # field :locked_at,  :type => Time 

    ## Token authenticatable 
    # field :authentication_token, :type => String 
    include Mongoid::Timestamps 

    field :name, type: String 
    validates :name, presence: true 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX } 
    index({ email: 1 }, { unique: true, name: "email_index" }) 
    field :rank, type: String 
    field :num_words, type: Integer 
    field :time_in_city, type: Time 
    field :last_logged_in, type: Time 
    field :points, type: Integer 
    has_many :sentences 

    attr_accessible :name, :email, :password, :password_confirmation, :remember_me 

end 

有人可以幫助我,並告訴我爲什麼get/set在某些情況下工作,但不在其他情況下工作? 我 使用蒙戈(1.7.0) 使用mongoid(2.5.0) 使用Rails(3.2.8)

+0

爲什麼你同時有'field:content','attr_accessible:content'和'attr_accessor:content'? –

回答

0

嘗試刪除attr_accessor :content,:num_votes,:last_submittedattr_accessor方法用於創建方法來簡單地設置和讀取對象中的實例變量。例如,對於attr_accessor :content,你最終的方法:

def content= (something) 
    @content = something 
end 

def content 
    @content 
end 

先不涉及save調用來更新數據庫,只是更新內存中的實例變量。

+0

謝謝。這工作。回覆晚了非常抱歉。 – user1710833

相關問題