Ruby on rails Mongoid-在某些情况下无法设置/获取属性
我有一个简单的模型类:Ruby on rails Mongoid-在某些情况下无法设置/获取属性,ruby-on-rails,mongoid,updatemodel,setattribute,Ruby On Rails,Mongoid,Updatemodel,Setattribute,我有一个简单的模型类: 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 bel
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)尝试删除
attr\u访问器:content、:num\u vots、:last\u submitted
。attr\u accessor
方法用于创建方法,以便简单地设置和读取对象中的实例变量。例如,对于attr\u accessor:content
,您将得到以下方法:
def content= (something)
@content = something
end
def content
@content
end
第一个不涉及更新数据库的
save
调用,只涉及对内存中实例变量的更新。为什么同时有字段:content
、attr\u accessible:content
和attr\u accessor:content
?