Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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
Sql 如何调用对rails模型中其他表的引用_Sql_Ruby On Rails_Reference - Fatal编程技术网

Sql 如何调用对rails模型中其他表的引用

Sql 如何调用对rails模型中其他表的引用,sql,ruby-on-rails,reference,Sql,Ruby On Rails,Reference,我有一个名为cause.rb的模型,它有一个名为pesquisa的范围,我想在表cause中查询该范围,这些字段是对其他表的引用。我想调用表places,它有一个来自模型cause的名为city的字段 原因表: class CreateCauses < ActiveRecord::Migration def change create_table :causes do |t| t.string :title, null: false t.text :de

我有一个名为
cause.rb
的模型,它有一个名为
pesquisa
的范围,我想在表cause中查询该范围,这些字段是对其他表的引用。我想调用表places,它有一个来自模型cause的名为city的字段

原因表:

class CreateCauses < ActiveRecord::Migration
  def change
    create_table :causes do |t|
      t.string :title, null: false
      t.text :description, null: false
      t.boolean :anonymous, default: false
      t.integer :count_likes, default: 0
      t.integer :count_dislikes, default: 0
      t.integer :count_visits, default: 0
      t.references :user
      t.references :category

      t.timestamps
    end
    add_index :causes, :user_id
    add_index :causes, :category_id
  end
end

class AddPlaceIdToCauses < ActiveRecord::Migration
  def change
    add_column :causes, :place_id, :integer
  end
end

如何解决这个问题?我试着用上面的
cause.place.city
调用表中的城市places,但什么都没有发生。

pesquisa到底是什么?无论哪种方式,您都需要将这些表连接在一起,请尝试在where子句前面添加
连接(:place=>:city)
。尽管如此,您仍然需要更新您的查询。pesquisa是一个文本字段标记,那么您可能无法通过
类别id
对其进行过滤,不是吗?尝试
连接(:category,:place=>:city)。where(“cities.name LIKE:p或categories.name LIKE:p”,p:%{pesquisa}%”
我假设category和city中的文本字段命名为
name
。完美我的朋友,工作得棒极了!
class CreatePlaces < ActiveRecord::Migration
  def change
    create_table :places do |t|
      t.string :description
      t.decimal :latitude
      t.decimal :longitude
      t.references :city

      t.timestamps
    end
    add_index :places, :city_id
  end
end
scope :pesquisa, ->(pesquisa) { where("cause.place.city like :p or category_id like :p ", p: "%#{pesquisa}%") }
default_scope :order => "id desc"