Rails:意外的 tSYMBEG 错误

Kri*_*nov 1 ruby activerecord ruby-on-rails rails-activerecord

我发现了几个关于同一主题的问题,但它们都有标点符号问题(遗漏或添加了逗号)。就我而言,迁移时出现以下错误

SyntaxError: /home/ /workspace/ /app/models/user.rb:27: 语法错误,意外的 tSYMBEG,期望 keyword_do 或 '{' 或 '('
has_many :roles, through :positions

用户模型:

class User < ActiveRecord::Base
  has_many :positions
  has_many :roles, through :positions
end
Run Code Online (Sandbox Code Playgroud)

榜样:

class Role < ActiveRecord::Base
  has_many :positions
  has_many :users, through :positions
end
Run Code Online (Sandbox Code Playgroud)

岗位模型:

class Position < ActiveRecord::Base
  belongs_to :role
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

知道可能是什么问题吗?谢谢!

pot*_*hin 6

through 这里应该是一个符号:

has_many :roles, through: :positions
Run Code Online (Sandbox Code Playgroud)

和这里:

has_many :users, through: :positions
Run Code Online (Sandbox Code Playgroud)

它是传递给has_many方法的哈希键,而不是单独的方法。

文档