【问题标题】:Mongoid relation not being persistedMongoid 关系没有被持久化
【发布时间】:2011-09-17 22:32:16
【问题描述】:
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
=> []

正如您在上面看到的,当尝试添加子文档时,它不会被保存。关于会发生什么的任何想法?

此外,这是一个 has_many / belongs_to 关系,当我将其更改为 has_and_belongs_to_many 时,它似乎工作正常。

【问题讨论】:

    标签: mongoid


    【解决方案1】:

    我猜你已经升级了 Mongoid,但还没有阅读 the upgrading docs

    , :autosave =&gt; true 添加到Account 与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
    

    这也是as a GitHub issue 提交的。啧啧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 2017-11-27
      • 2012-01-09
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2015-07-27
      相关资源
      最近更新 更多