-1

for my snippet below for rendering the json

def show
   @product = Product.find(params[:id])
      render json: @product.to_json(:include => { :items => { :only => [:id, 
:description] }}) 
end

While rendering the json for has many attributes model I am getting invalid statement as follows:

(Mysql2::Error: Unknown column 'items.product_id' in 'where clause': SELECT `items`.* FROM `items` WHERE `items`.`product_id` = 1):

While I have a column called products_id for the foreign key instead of product_id. I need to throw statement as products_id without changing the column name in database.

1 Answers1

1

In your Product model you should have something like has_many :items. This line creates an association with default foreign key product_id. To change foreign key you need to change this line to has_many :items, foreign_key: :products_id.

P. Boro
  • 716
  • 3
  • 12
  • 1
    Since we're speculating without seeing the actual model code - are you sure that's all? What about `belongs_to` association in `Item` model? – Bartosz Pietraszko Oct 10 '18 at 12:33
  • 1
    Pretty sure it will fix the problem with the unknown column and will allow OP to render this JSON. `belongs_to` is another issue. – P. Boro Oct 10 '18 at 12:46