Rails: belongs_to with conditions/scope throwing syntax error

萝らか妹 提交于 2019-12-02 05:33:44

unexpected '\n', expecting =>

You need to switch the order of 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

As you can see, the scope should be the second parameter and options should be the third parameter.

This should work

belongs_to :from, -> { where owned: true }, class_name: 'Company', foreign_key: 'from_id'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!