Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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_Ruby On Rails 3_Sqlite - Fatal编程技术网

Ruby on rails Rails正在尝试加入表。找不到列?

Ruby on rails Rails正在尝试加入表。找不到列?,ruby-on-rails,ruby,ruby-on-rails-3,sqlite,Ruby On Rails,Ruby,Ruby On Rails 3,Sqlite,我试图从搜索结果返回一个带有游戏和游戏图片的json响应 我有以下代码: 控制器: def search @games = Game.search(params[:query]).order("created_at DESC").joins(:game_image).where( game_image: { primary: true }) respond_to do |format| format.html format.json { render

我试图从搜索结果返回一个带有游戏和游戏图片的json响应

我有以下代码:

控制器:

  def search
    @games = Game.search(params[:query]).order("created_at DESC").joins(:game_image).where( game_image: { primary: true })
    respond_to do |format|
      format.html
      format.json { render :json => {:message => "Success", :results => @games}}
    end
  end
class GameImage < ActiveRecord::Base
  attr_accessible :game_id, :image, :primary , :title
  belongs_to :game
  mount_uploader :image, GameImageUploader
end

class Game < ActiveRecord::Base
  attr_accessible :activated, :description, :metascore, :metascore_url, :name, :pegi_rating, :youtube_url, :release_date , :game_link_attributes , :game_platform_attributes , :edition_attributes , :game_edition_attributes, :game_image_attributes
  validates :name, presence: true

  has_many :game_link
  has_many :shop , through: :game_link
  has_many :game_platform
  has_many :game_genre
  has_many :game_edition
  has_many :edition , through: :game_edition
  has_many :game_image

  accepts_nested_attributes_for :game_image, allow_destroy: true
  accepts_nested_attributes_for :game_link, allow_destroy: true
  accepts_nested_attributes_for :game_platform, allow_destroy: true
  accepts_nested_attributes_for :edition, allow_destroy: true
  accepts_nested_attributes_for :game_edition, allow_destroy: true

  def self.search(query)
    where("name like ?", "%#{query}%")
  end
end
class CreateGameImages < ActiveRecord::Migration
  def change
    create_table :game_images do |t|
      t.integer :game_id
      t.string :image
      t.string :title
      t.boolean :primary

      t.timestamps
    end
  end
end
搜索功能可以正常工作,只是一个简单的
查询,类似于sql中的
查询。现在我正在尝试加入game_image表,该表应该只选择主图像

型号:

  def search
    @games = Game.search(params[:query]).order("created_at DESC").joins(:game_image).where( game_image: { primary: true })
    respond_to do |format|
      format.html
      format.json { render :json => {:message => "Success", :results => @games}}
    end
  end
class GameImage < ActiveRecord::Base
  attr_accessible :game_id, :image, :primary , :title
  belongs_to :game
  mount_uploader :image, GameImageUploader
end

class Game < ActiveRecord::Base
  attr_accessible :activated, :description, :metascore, :metascore_url, :name, :pegi_rating, :youtube_url, :release_date , :game_link_attributes , :game_platform_attributes , :edition_attributes , :game_edition_attributes, :game_image_attributes
  validates :name, presence: true

  has_many :game_link
  has_many :shop , through: :game_link
  has_many :game_platform
  has_many :game_genre
  has_many :game_edition
  has_many :edition , through: :game_edition
  has_many :game_image

  accepts_nested_attributes_for :game_image, allow_destroy: true
  accepts_nested_attributes_for :game_link, allow_destroy: true
  accepts_nested_attributes_for :game_platform, allow_destroy: true
  accepts_nested_attributes_for :edition, allow_destroy: true
  accepts_nested_attributes_for :game_edition, allow_destroy: true

  def self.search(query)
    where("name like ?", "%#{query}%")
  end
end
class CreateGameImages < ActiveRecord::Migration
  def change
    create_table :game_images do |t|
      t.integer :game_id
      t.string :image
      t.string :title
      t.boolean :primary

      t.timestamps
    end
  end
end

看看你们的关系<代码>游戏有很多游戏图片
(你在游戏图片中看到复数形式)。在你的模型中修复这个问题,然后你的查询应该是:
Game.search(params[:query]).order(“created_at DESC”).join(:Game_images.),where(Game_images:{primary:true})
(同样是Game_images的复数形式)你好,我刚刚试过这个。但我通常用单数。使用复数原因
未找到名称游戏\u图像的关联。定义好了吗?
看一看,你会发现你应该使用复数版本。在手册中:
另一个模型的名称在声明has\u many关联时是复数的。
Soo,在game.rb中使用:
has\u many:game\u images
接受:game\u images的嵌套属性,允许销毁:true
并像第一条评论中那样重写查询。谢谢我这么做,但现在我得到
游戏中找不到名为“game_image”的关联;也许你拼错了?
你有没有把你的
.join(:game\u images)
也改成复数?