I created a model for a thing called a Lesson that has :content and a :user_id. For early builds of this app, I want the content to be changing, based on my entry, and for the user_id to always = 1 so that it’s clean in the DB and there’s not a nil value.
How do I go about this?
In my lessons_controller.rb I have this:
def create
@lesson = Lesson.new(params[:lesson])
if @lesson.save
... do something
else
... do something else
I’m guessing this would be the best place to define that the user_id = 1 but how should I go about that?
You can just set
@lesson.user_id = 1in the line after you create it withnew, and before yousaveit.Another way to do it would be to set a hook in the lesson model –