Ruby on rails Rails:属于,条件/作用域抛出语法错误
我想挑选拥有的公司。我尝试了多种组合,新的,rails 3,旧的,。。。所有这些都抛出了相同的语法错误Ruby on rails Rails:属于,条件/作用域抛出语法错误,ruby-on-rails,ruby-on-rails-5,conditional-associations,Ruby On Rails,Ruby On Rails 5,Conditional Associations,我想挑选拥有的公司。我尝试了多种组合,新的,rails 3,旧的,。。。所有这些都抛出了相同的语法错误意外的'\n',预期为=> 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)
意外的'\n',预期为=>
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年前似乎有人问过这个问题,但似乎没有明确的答案!还有别的办法吗?谷歌没有返回所属的有条件的或有范围的的相关结果
我需要准确地回答,但准确的答案也会引发语法错误
意外的“\n”,应为=>
您需要使用选项切换范围
的顺序
# 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
如您所见,范围
应该是第二个参数,选项
应该是第三个参数
这应该行得通
belongs_to :from, -> { where owned: true }, class_name: 'Company', foreign_key: 'from_id'
意外的“\n”,应为=>
您需要使用选项切换范围
的顺序
# 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
如您所见,范围
应该是第二个参数,选项
应该是第三个参数
这应该行得通
belongs_to :from, -> { where owned: true }, class_name: 'Company', foreign_key: 'from_id'