Nick
Nick

Reputation: 3090

PG::UndefinedTable: ERROR: missing FROM-clause

As part of a form I have in view:

<%= form_for(@invitation, method: :post, url: addinvite_path) do |f| %>
  ...
  <label for="email", title="email"></label>
  <%= email_field_tag :email, nil, placeholder: 'Email', autocomplete: 'off', required: true %><br>
  ...

The invitations controller method that processes a submission of the form:

def create
  @user = User.find(email: params[:email])
  ...

Submitting the form produces an error pointing to the @user line:

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "id"

I'm not sure what causes this error. Placing the debugger before the @user line confirms there's a value for params[:email]. But also in the debugger if I enter User.find(email: params[:email]) it returns the above enter. What could be causing the error?

Upvotes: 2

Views: 731

Answers (3)

Dinesh Saini
Dinesh Saini

Reputation: 2916

Here is the updated copy:

For Single Record

def create
  @user = User.where(email: params[:email]).first
end

For Multi Records

 def create
   @user = User.where(email: params[:email])

Upvotes: 2

Pavan
Pavan

Reputation: 33542

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "id"

By default find will look for id. You need to use find_by

@user = User.find_by(email: params[:email])

Upvotes: 2

Rajarshi Das
Rajarshi Das

Reputation: 12320

You can try find_by instead of find

def create
  @user = User.find_by(email: params[:email])

Upvotes: 3

Related Questions