I have 2 classes feed.rb and ad.rb Ad has_many Feed Feed belongs_to Ad
class Feed
include Mongoid::Document
belongs_to :ad , optional: true
before_save :generate_timestamp
validates_presence_of :name
field :name, type: String
field :insertdate, type: DateTime
field :addata, type: Hash
def generate_timestamp
self.insertdate = DateTime.now
end
end
class Ad
include Mongoid::Document
has_many :feed
before_save :generate_timestamp
validates_presence_of :name
field :name, type: String
field :insertdate, type: DateTime
field :creative, type: String
def generate_timestamp
self.insertdate = DateTime.now
end
end
In the controller, it saves the ad as new object with list of Feeds
def addad
@feeds = Feed.all
if request.post?
ad = Ad.new
ad.name = params['name']
ad.creative = params['creative']
ad.feed << @feeds
render plain: ad.inspect
ad.save
end
end
When I check the DB and print out ad object, there is no feed attribute
#<Ad _id: 58a11ef023040c07963ef885, name: "this is testing", insertdate: nil, creative: "Sample">
Is there anything I missed?
Thanks.
If you are use mogodb(mogoid) then you have to specify association in different manner like below
class Article include Mongoid::Document field :name field :content field :published_on, :type => Date validates_presence_of :name embeds_many :comments referenced_in :author end
please go thought this link for better understanding http://railscasts.com/episodes/238-mongoid