Mongoid - Unable to set/get attribute in some cases
-
11-12-2019 - |
Question
I have a simple model class:
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
I am trying to set the content attribute like this:
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)
But if I do
sen[:content] = "Hello" - This updates the attribute
sen.write_attribute(:content,"Hello") - This updates the attribute
I am confused as to what is going on here, and why in some cases, my update works while in others it doesnt. I have the same problem with get attribute as well. sen.content returns nil, but sen[:content] returns the correct content I have another model class, and in this case, all four methods mentioned above to get/set attributes work on all the attributes
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
Can someone help me out and tell me why the get/set works in certain cases but not in others ? I am Using mongo (1.7.0) Using mongoid (2.5.0) Using rails (3.2.8)
Solution
Try removing attr_accessor :content,:num_votes,:last_submitted
. The attr_accessor
method is used to creates methods to simply set and read an instance variable in the object. For example, for attr_accessor :content
, you end up with the methods:
def content= (something)
@content = something
end
def content
@content
end
The first doesn't involve a save
call to update the db, just an update to the instance variable in memory.