Answer the question
In order to leave comments, you need to log in
How to write validation for checking ID?
There are two models: User and Post.
The posts table has a user_id field. How to check it when adding a post, they say, is there a record with such an ID in the users table or is it absent?
Yes validates_associated
, but this helper seems to perform a slightly different task.
Is this really the only true option?
Answer the question
In order to leave comments, you need to log in
@user = User.find(params[:user_id])
@post = @user.posts.build(post_params)
@post.save
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question