Rails - TypeError:更新时,nil不是符号,也不是字符串

Bit*_*ise 3 ruby-on-rails

我正在尝试更新属性.它是一个默认值为false的布尔值.在我的方法中,我进行查询以找到合适的用户.然后我尝试通过将其翻转owner boolean为true 来更新该用户.这是一个非常奇怪的错误,因为它正在更新方法,但它也发出了这个错误TypeError: nil is not a symbol nor a string

我只是想知道我在这里做错了什么.

调节器

def update
  membership = current_account.account_memberships.find_by(user_id: params[:id])
  membership.update(owner: true)
end
Run Code Online (Sandbox Code Playgroud)

HTML

<%= link_to "Owner", account_membership_path(user), {
  class: "icon icon-owner-upgrade",
  method: :patch,
} %>
Run Code Online (Sandbox Code Playgroud)

小智 7

如果您尝试更新依赖模型中的记录 - 它可能只有"foreing key",但没有"主键".如果你像这样添加一行"self.primary_key",问题就解决了:

class YourDependentRecord <ApplicationRecord
  self.primary_key ='user_id'
结束

其中'user_id' - 是主模型中的主键.