A
A
Anton2016-07-22 11:01:07
Ruby on Rails
Anton, 2016-07-22 11:01:07

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

1 answer(s)
A
Andrey Demidenko, 2016-07-22
@hummingbird

@user = User.find(params[:user_id])
@post = @user.posts.build(post_params)
@post.save

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question