2011-09-17 39 views
5
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 

-Mongoid关系没有持续

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 
=> [] 

正如你看到的上面,尝试添加子文档它不会保存时。任何想法可能会发生什么?

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

回答

7

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

将账号与Submission的关系加上, :autosave => true

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。 Tsk tsk。