Rails: belongs_to with conditions/scope throwing syntax error

前端 未结 1 983
悲&欢浪女
悲&欢浪女 2021-01-25 16:13

I want to pick companies that are owned. I tried multiple combinations, new, rails 3, old school,... all of which are throwing the same syntax error unexpected \'

相关标签:
1条回答
  • 2021-01-25 16:41

    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'
    
    0 讨论(0)
提交回复
热议问题