I am trying to return a json response with a game and a game_image from a search result.
I have the following code:
Controller:
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
the search function works and is just a simple LIKE
query in sql. Now I am trying to join the game_image table which should only pick the primary image.
Models:
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 migrate file for 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
Problem:
Saying no such column altough this column exists. Can someone help me? Thanks
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
Aucun commentaire:
Enregistrer un commentaire