Bitwise
Bitwise

Reputation: 8461

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,
} %>

Upvotes: 6

Views: 18617

Answers (1)

Vadim
Vadim

Reputation: 106

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.

Upvotes: 9

Related Questions