Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid relation not being persisted

Tags:

mongoid

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: "[email protected]", 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 trying to add a child document it doesn't get saved. Any ideas as to what could be going on?

Also- this is a has_many / belongs_to relationship, and when I change it to has_and_belongs_to_many it seems to work fine.

like image 302
Chris Bolton Avatar asked Sep 17 '11 22:09

Chris Bolton


1 Answers

My guess is that you've upgraded Mongoid, but haven't read the upgrading docs.

Add , :autosave => true to Account's 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 submitted as a GitHub issue. Tsk tsk.

like image 164
nickh Avatar answered Sep 30 '22 13:09

nickh