Rails试图加入一张桌子。找不到列?

时间:2015-05-23 14:18:50

标签: ruby-on-rails ruby ruby-on-rails-3 sqlite

我试图通过搜索结果中的游戏和game_image返回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

搜索功能有效,在sql中只是一个简单的LIKE查询。现在我正在尝试加入game_image表,该表只应该选择主图像。

型号:

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

db为game_image迁移文件:

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

问题:

尽管不存在此列,但此列仍然存在。有人能帮我吗?感谢

SQLite3::SQLException: no such column: game_image.primary: SELECT "games".* FROM "games" INNER JOIN "game_images" ON "game_images"."game_id" = "games"."id" WHERE "game_image"."primary" = 't' AND (name like '%di%') ORDER BY created_at DESC

0 个答案:

没有答案