13 ms·
You could do this as well: Repo.insert! %Post{title: "Awesome Post", slug: "awesome-post", category_id: 3} If you don't need flexibility. Well, usually that
by out_of_protocol 9y ago
You could do this as well:
Repo.insert! %Post{title: "Awesome Post", slug: "awesome-post", category_id: 3}
If you don't need flexibility. Well, usually that's bad idea - you'd want to carefully handle what's can be mass-assigned and what should be carefully set by hand.
In case of ecto, you'd do:
%Post{user_id: current_user.id}
|> Post.changeset(attributes)
|> Repo.insert
In case of rails/AR you'll use strong params (or alternatives)
Post.create(post_params.merge(user_id: current_user.id))
def post_params
params.require(:post).permit(:title, :slug, : category_id)
end