2012-10-01 3 views
1

경우에 속성 취득/설정할 수 없습니다Mongoid 나는 간단한 모델 클래스가

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 

가 나는 경우에 여기에가는 이유 것과 같은 혼란 스러워요, 내 업데이트 작동합니다. 나는 속성을 가져 오는 것과 같은 문제가있다. 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이 특정 경우에는 작동하지만 다른 곳에서는 작동하지 않는 이유를 말해 줄 수 있습니까? mongoid (2.5.0)를 사용 몽고 (1.7.0)를 사용하여 I 오전 사용하여 레일 (3.2.8)

+0

왜 당신은'field : content','attr_accessible : content' 및'attr_accessor : content'를 동시에 가지고 있습니까? –

답변

0

attr_accessor :content,:num_votes,:last_submitted을 제거하십시오. attr_accessor 메서드는 개체의 인스턴스 변수를 간단히 설정하고 읽는 메서드를 만드는 데 사용됩니다. 예를 들어, attr_accessor :content을 위해, 당신은 방법으로 끝날 :

def content= (something) 
    @content = something 
end 

def content 
    @content 
end 

가 첫 번째 DB를 메모리에 인스턴스 변수 그냥 업데이 트를 업데이트 할 save 전화를 포함하지 않습니다.

+0

감사. 그게 효과가 있었어. 답장을 늦게 보내서 미안해. – user1710833