我有一个简单的模型类:
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
我对此处发生的事情感到困惑,为什么在某些情况下,我的更新有效,而在其他情况下则不然。我对get属性也有同样的问题。 sen.content返回nil,但sen [:content]返回正确的内容 我有另一个模型类,在这种情况下,上面提到的所有四个方法来获取/设置属性都适用于所有属性
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+\-.]+@[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在某些情况下有效而在其他情况下无效吗? 我是 使用mongo(1.7.0) 使用mongoid(2.5.0) 使用rails(3.2.8)
答案 0 :(得分:0)
尝试删除attr_accessor :content,:num_votes,:last_submitted
。 attr_accessor
方法用于创建方法以简单地设置和读取对象中的实例变量。例如,对于attr_accessor :content
,您最终会得到以下方法:
def content= (something)
@content = something
end
def content
@content
end
第一个不涉及更新数据库的save
调用,只是对内存中实例变量的更新。