Domanda

Ho una semplice classe di modello:

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
.

Sto cercando di impostare l'attributo del contenuto in questo modo:

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)
.

ma se faccio

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

Sono confuso su cosa sta succedendo qui, e perché in alcuni casi, il mio aggiornamento funziona mentre negli altri non lo fa.Ho lo stesso problema con Attributo Get. sen.content ritorna Nil, ma Sen [: Content] restituisce il contenuto corretto Ho un'altra classe del modello, e in questo caso, tutti e quattro i metodi menzionati sopra per ottenere / impostare gli attributi lavorano su tutti gli attributi

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
.

Qualcuno può aiutarmi e dirmi perché il Get / Set funziona in alcuni casi ma non in altri? sono Usando il mongo (1.7.0) Usando Mongoid (2.5.0) Usando rotaie (3.2.8)

È stato utile?

Soluzione

Prova a rimuovere attr_accessor :content,:num_votes,:last_submitted.Il metodo attr_accessor viene utilizzato per creare metodi per impostare e leggere una variabile di istanza nell'oggetto.Ad esempio, per attr_accessor :content, si finisce con i metodi:

def content= (something)
    @content = something
end

def content
    @content
end
.

Il primo non coinvolge una chiamata save per aggiornare il DB, solo un aggiornamento alla variabile di istanza in memoria.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top