[Help] Need help with first_or_create. Never finds existing Record

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.

Yeah the sql you posted will not work. First, find the record with @project = current_user.projects.where(id: params[:id]).first_or_initialize and then set the attributes as desired.

I have a problem with a form that either updates or creates depending on

whether the “project” exists already.

How do you want to determine whether the project exists or not?

Im using the def create

def create

puts params.inspect

@project = current_user.projects.where(id:

params[:id]).first_or_create(project_params)

[snip]

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]]

This shows that params[:id] was nil. Were you expecting that?

Fred