Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:属于,条件/作用域抛出语法错误_Ruby On Rails_Ruby On Rails 5_Conditional Associations - Fatal编程技术网

Ruby on rails Rails:属于,条件/作用域抛出语法错误

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)

我想挑选拥有的公司。我尝试了多种组合,新的,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) }
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'