2
votes

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?

3
Try using with find_by. @user = User.find_by(email: params[:email]) - Pavan

3 Answers

3
votes

You can try find_by instead of find

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

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])
2
votes

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