I am using Ruby on Rails 4.1 and I would like to know if it is good and what are the drawbacks of creating a associated record just after the associator record creation. That is, for example, I have the Article
class and the Comment
class that belongs_to
Article
and I would like to create a "default" comment just after an article is created.
Probably I can use a callback method to accomplish that, but are there other approaches to accomplish what I looking for? What should I look at to be "balanced" (eg: class/module dependencies)?
You are looking for something along the lines of...
Class Article
has_many :comments
after_create :create_first_comment!
def create_first_comment!
comments.create
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.