Mongoid attitude is not preserved

class Account
  include Mongoid::Document
  include Geocoder::Model::Mongoid
  geocoded_by :zip
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

  before_validation :pass_confirm
  after_validation :geocode_check
  before_create :assign_subs

  field :email, type: String
  field :type, type: String
  field :zip, type: String
  field :oldzip, type: String
  field :coordinates, type: Array
  field :latitude, type: Float
  field :longitude, type: Float
  auto_increment :num, collection: :account_nums

  index :num, unique: true

  has_many :submissions

  mount_uploader :photo, PhotoUploader

  def self.find_by_num(num)
    Account.where(num: num).first
  end

  protected

  def pass_confirm
    self.password_confirmation ||= self.password
  end

  def geocode_check
    if self.oldzip != self.zip
      self.oldzip = self.zip
      self.geocode
    end
  end

  def assign_subs
    binding.pry
    Submission.where(email: self.email).each do |sub|
      sub.zip = self.zip
      self.submissions << sub
    end
  end

end

-

class Submission
  include Mongoid::Document
  include Mongoid::Search
  include Mongoid::Timestamps::Created
  include Geocoder::Model::Mongoid
  geocoded_by :zip

  before_validation :fix_rate
  after_validation :geocode

  search_in :message, tags: :name

  field :email, type: String
  field :rate, type: String
  field :message, type: String
  field :type, type: String
  field :zip, type: String
  field :coordinates, type: Array
  field :latitude, type: Float
  field :longitude, type: Float
  auto_increment :num, collection: :submission_nums

  index :num, unique: true

  has_and_belongs_to_many :tags
  belongs_to :account

  mount_uploader :photo, PhotoUploader

  protected

  def fix_rate
    self.rate = self.rate.sub(/[^\d]*/, '').sub(/(\d*).*/, '\1')
  end

end

-

pry(#<Account>)> self.submissions << Submission.first
=> [#<Submission _id: 4e751df86066252059000054, _type: nil, created_at: 2011-09-17 22:23:52 UTC, _keywords: ["tfnwuaty"], email: "krisbltn@gmail.com", rate: "49", message: "tfnwuaty", type: "person", zip: nil, coordinates: nil, latitude: nil, longitude: nil, num: 1, tag_ids: [], account_id: BSON::ObjectId('4e751e0d6066252059000059'), photo: "lawrence.jpg">]
pry(#<Account>)> self.submissions
=> []

As you see above, when you try to add a child document, it is not saved. Any ideas on what might be happening?

Also, this is the has_many / belongs_to relation, and when I change it to has_and_belongs_to_many, it works fine.

+5
source share
1 answer

I assume that you updated Mongoid, but did not read the update documents .

Add , :autosave => trueto account relation with Submission.

class Account
  include Mongoid::Document
  has_many :submissions, :autosave => true
end

class Submission
  include Mongoid::Document
  belongs_to :account
end

Account.delete_all
Submission.delete_all

Submission.create
account = Account.new
account.submissions << Submission.first
account.save
Submission.first.account == account

This was also introduced as a GitHub issue . Tsk tsk.

+7
source

All Articles