1

I want to get a list of all the reviews that belong to a, which belongs a category. I'm getting an error when trying to get a list of all the reviews in a JSON file (it's a API application): "/Code/Rails/videogame_reviews/app/controllers/reviews_controller.rb:6: syntax error, unexpected tIVAR, expecting '(' @reviews = @category.@game.reviews ^".

Could anybody explain why this is failing? @reviews = Reviews.all is working but shows all the reviews.

This the is ReviewsController

def index
  @game = Game.find(params[:game_id])
  @category = Category.find(params[:category_id])
  @reviews = @category.@game.reviews
  respond_to do |f|
    f.html
    f.json { render json: { category: @category, game: @game, reviews: @reviews} }
  end
end

I've made a copy from the GamesController index which is working

 def index
  @category = Category.find(params[:category_id])
  @games = @category.games
  respond_to do |f|
    f.html
    f.json { render json: { category: @category, games: @games } }
  end
end

Here is the Reviews model

class Review < ActiveRecord::Base
  belongs_to :game
  validates_presence_of :comment
end

This is the Game model

class Game < ActiveRecord::Base
  belongs_to :category
  has_many :reviews
  validates_presence_of :title, :category
end

And this is the Category model

class Category < ActiveRecord::Base
  validates_presence_of :name
  has_many :games
end

Here is the routes for the reviews

resources :categories do
  resources :games do
     resources :reviews
  end
end
Samuel
  • 513
  • 3
  • 17

2 Answers2

0

The syntax for @reviews = @category.@game.reviews is incorrect. I'm guessing you want all reviews in a category for a certain game? If so, try the following:

Review.joins(games: :category).where(categories: {id: @category.id}, games: {id: @game.id})
Anthony E
  • 11,072
  • 2
  • 24
  • 44
  • it is comment rather than answer. – Ilya Apr 30 '16 at 10:09
  • I've tried your code. I run through the syntax but gives me an eror on the JSON part: **Association named 'games' was not found on Review; perhaps you misspelled it?** – Samuel Apr 30 '16 at 10:36
  • Sorry, should be singular: `Review.joins(game: :category).where(categories: {id: @category.id}, games: {id: @game.id})` – Anthony E Apr 30 '16 at 11:20
  • @Ilya Are you the one mass-downvoting my posts? Care to explain why? – Anthony E Apr 30 '16 at 14:27
  • @AnthonyE, what are you say about? I've just downvoted this port in first redaction, cause it not consisted solution to resolve it, only note about incorrect syntax. Then, about 1 hour ago I've untouched downvote button since you edited your answer. – Ilya Apr 30 '16 at 14:41
  • @Ilya You know what I'm talking about. If you could fix it it would be appreciated, thanks. – Anthony E Apr 30 '16 at 14:55
  • @AnthonyE, dude, somebody also downvoting some my posts today, but I dont blame you, it may be anybody. Why you blame exactly me? It is very interesting to know your logic – Ilya Apr 30 '16 at 15:06
0

The line,

@reviews = @category.@game.reviews

wont work because @game is not a category method. @game is a variable. You can use the activeRecord query in the above answer to get all the reviews belonging to a particular category and game.

@reviews = Review.joins(games: :category).where(categories: {id: @category.id}, games: {id: @game.id})
margo
  • 2,927
  • 1
  • 14
  • 31