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?
精彩评论