I'm wondering how would one go about prepopulating certain data table for a new user? Let's say I have a table with posts and a table with likes, and when new a user registers I'd like to make it so that certain posts are automatically liked upon registration. Would appreciate any guidance and suggestion.
There's a couple options for this generally:
class User < ApplicationRecord has_many :likes after_create :create_default_likes private def create_default_likes # Create your likes here [1,2,3].each do |id| likes.create post_id: id end end end
For the controller version, you'd basically do the same thing just after the
if user.save. If you use something like Devise, you'll have to override the controller to do that.
Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.