Rails:属于_to,条件/范围抛出语法错误

2023-12-01

我想选择拥有的公司。我尝试了多种组合,新的、Rails 3、旧的学校、...所有这些都抛出相同的语法错误unexpected '\n', expecting =>

belongs_to :from, class_name: 'Company', foreign_key: 'from_id', -> { where owned: true }
belongs_to :from, class_name: 'Company', foreign_key: 'from_id', -> { where(owned: true) }
belongs_to :from, class_name: 'Company', foreign_key: 'from_id', -> { where(:owned => true) }
belongs_to :from, class_name: 'Company', foreign_key: 'from_id', condition: { where(:owned => true) }

好像3年前就有人问过here,但似乎没有明确的答案!还有其他办法吗? Google 没有返回相关结果belongs_to with conditions or with scope

我需要准确地做this,但确切的答案也会引发语法错误......


意外的 '\n',期待 =>

你需要switch的顺序scope with options

# File activerecord/lib/active_record/associations.rb, line 1514
def belongs_to(name, scope = nil, options = {})
  reflection = Builder::BelongsTo.build(self, name, scope, options)
  Reflection.add_reflection self, name, reflection
end

如您所见,scope应该是第二个参数 and options应该是第三个参数.

这应该有效

belongs_to :from, -> { where owned: true }, class_name: 'Company', foreign_key: 'from_id'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails:属于_to,条件/范围抛出语法错误 的相关文章

随机推荐