Error while implementing unions in ruby ββon rails 3.2
I created two tables in the database i.e. products and shopping_list. I gave the products a foreign key reference for shopping_list, i.e. product.shopping_list_id.
I am trying to implement joins in ruby ββon rails, but I am getting the error
my route.rb file looks like this
Shop::Application.routes.draw do
resources :shopping_lists do
member do
post 'add'
get 'delete'
end
collection do
get 'add'
get 'joins'
get 'list'
post 'get_shopping_lists'
end
end
resources :shopping_lists
# ---- Contact Routes ------
resources :products do
member do
post 'add'
get 'delete'
end
collection do
get 'add'
get 'list'
post 'get_products'
end
end
resources :products
my product.rb
class Product < ActiveRecord::Base
attr_accessible :id, :shopping_list_id, :product_name, :product_category, :quantity, :status
# ------- ASSOCIATIONS --------
belongs_to :shopping_list
# ------ VALIDATIONS ------
validates_presence_of :id, :product_name
validates_uniqueness_of :id
# -------- SCOPES ----------
# scope :not_deleted, where("products.deleted = 0")
# default_scope not_deleted
end
my shopping_list.rb
class ShoppingList < ActiveRecord::Base
attr_accessible :id, :shopping_list_name, :shopping_list_status, :total_items, :created_by, :last_updated_by
# ------- ASSOCIATIONS --------
has_many :products
# ------ VALIDATIONS ------
validates_presence_of :id, :shopping_list_name
validates_uniqueness_of :id
# -------- SCOPES ----------
# scope :not_deleted, where("shoppinglistsqa.deleted = 0")
# default_scope not_deleted
end
the request I am trying to implement is
select shopping_lists.shopping_list_name,shopping_lists.id,products.product_name,products.product_category
from products,shopping_lists
where shopping_lists.id=products.shopping_list_id;
Could you help me.
+3
source to share
1 answer
Just do something like:
@products = Product.includes(:shopping_lists)
# if you want to get all products + their shopping list
@shopping_list = ShoppingList.includes(:product).find(params[:id])
# if you want to return just 1 shopping_list + it product
@product = Product.includes(:shopping_lists).find(params[:id])
# if you want to return a specific product + all of it shopping lists
For more information on database queries, see here .
+3
source to share