Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - TypeError: nil is not a symbol nor a string, when updating

I'm trying to update an attribute. It's a boolean with a default of false. In my method I make a query to find the right user. then I try to update that user by flipping the owner boolean to true. It's a really strange error I'm getting because it's updating the method but it also sending out this error TypeError: nil is not a symbol nor a string

I'm just wondering what I'm doing wrong here.

Controller

def update
  membership = current_account.account_memberships.find_by(user_id: params[:id])
  membership.update(owner: true)
end

HTML

<%= link_to "Owner", account_membership_path(user), {
  class: "icon icon-owner-upgrade",
  method: :patch,
} %>
like image 708
Bitwise Avatar asked Nov 16 '16 03:11

Bitwise


1 Answers

If you are trying to update the record in a dependent model, it possibly has only a foreign key, but no primary key. The problem can be solved if you add a line self.primary_key like this:

class YourDependentRecord < ApplicationRecord  
  self.primary_key = 'user_id'
end

where 'user_id' is the primary key in the master model.

like image 132
Vadim Avatar answered Oct 31 '22 13:10

Vadim