Ruby on Rails Friday, October 30, 2015

I have a problem with a form that either updates or creates depending on
whether the "project" exists already.

Im using the def create

def create
puts params.inspect
@project = current_user.projects.where(id:
params[:id]).first_or_create(project_params)
puts params.inspect
if @project.save
flash[:success] = "Project created!"
redirect_to root_url
else
flash[:success] = "Project not created!"
redirect_to root_url
end
end

Problem is it never finds the existing record.

generated SQL is


SELECT "projects".* FROM "projects" WHERE "projects"."user_id" = ? AND
"projects"."id" IS NULL ORDER BY "projects"."created_at" DESC LIMIT 1
[["user_id", 1]]
Im sending the parameters in wrong but i'm not sure of the solution.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/566af89afa123c972dce3df6d89de911%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment