开发者

How to validate presence of associated object in mongoDB?

开发者 https://www.devze.com 2023-03-08 04:03 出处:网络
In my application I have Link model like this: class Link开发者_C百科 include Mongoid::Document field :url, :type => String

In my application I have Link model like this:

class Link开发者_C百科
  include Mongoid::Document

  field :url, :type => String
  validates_presence_of :url
  belongs_to :link_bucket  
end

and LinkBucket model, which is inherited from FeedItem model (in my app FeedItem could contain links, message, audio_track and so on, this is why I use inheritance).

class LinkBucket < FeedItem
    has_many :links
end

So how can I verify if there is a link before I create LinkBucket object?


You can't make the association unless the objects exist or are being created at the time. But you can create a Link without having an associated LinkBucket, and then later create the LinkBucket and associate them. In other words, only create a LinkBucket when you are sure you have a Link and you need to create one. Does that help?

0

精彩评论

暂无评论...
验证码 换一张
取 消