I have the following Database structure.
class Category < ActiveRecord::Base
has_many :member_categories
has_many :members, :through => :member_categories
end
class Member < ActiveRecord::Base
has_many :member_categories
has_many :categories, :through => :member_categories
end
class MemberCategory < ActiveRecord::Base
self.table_name = "member_categories"
belongs_to :member
belongs_to :category
end
I can get any member categories as the following statement
Member.first.categories
there i find categories assigned to the member. I need to select the categories those are not assigned to the member. How can i write a scope to accomplish this.
Please advise, thanks in Advance.
This isn't a suitable job for a scope. Just write a method to find all categories with IDs not in the set of category IDs associated with the given member:
class Member
has_many :member_categories
has_many :categories, through: :member_categories
def not_categories
Category.where('id not in (?)', categories.pluck(:id))
end
end
Related
I have users table, books table and books_users join table. In the users_controller.rb I am trying extract the users who have filtered_books. Please help me to resolve that problem.
user.rb
has_many :books_users, dependent: :destroy
has_and_belongs_to_many :books, join_table: :books_users
book.rb
has_and_belongs_to_many :users
books_user.rb
belongs_to :user
belongs_to :book
users_controller.rb
def filter_users
#filtered_books = Fiction.find(params[:ID]).books
#users = **I want only those users who have filtered_books**
end
has_and_belongs_to_many does not actually use a join model. What you are looking for is has_many through:
class User < ApplicationRecord
has_many :book_users
has_many :books, through: :book_users
end
class Book < ApplicationRecord
has_many :book_users
has_many :users, through: :book_users
end
class BookUser < ApplicationRecord
belongs_to :book
belongs_to :user
end
If you want to add categories to books you would do it by adding a Category model and another join table. Not by creating a Fiction model which will just create a crazy amount of code duplication if you want multiple categories.
class Book < ApplicationRecord
has_many :book_users
has_many :users, through: :book_users
has_many :book_categories
has_many :categories, through: :book_categories
end
class BookCategory < ApplicationRecord
belongs_to :book
belongs_to :category
end
class Category < ApplicationRecord
has_many :book_categories
has_many :books, through: :book_categories
end
If you want to query for users that follow a certain book you can do it by using an inner join with a condition on books:
User.joins(:books)
.where(books: { title: 'Lord Of The Rings' })
If you want to get books that have a certain category:
Book.joins(:categories)
.where(categories: { name: 'Fiction' })
Then for the grand finale - to query users with a relation to at least one book that's categorized with "Fiction" you would do:
User.joins(books: :categories)
.where(categories: { name: 'Fiction' })
# or if you have an id
User.joins(books: :categories)
.where(categories: { id: params[:category_id] })
You can also add an indirect association that lets you go straight from categories to users:
class Category < ApplicationRecord
# ...
has_many :users, though: :books
end
category = Category.includes(:users)
.find(params[:id])
users = category.users
See:
The has_many :through Association
Joining nested assocations.
Specifying Conditions on Joined Tables
From looking at the code i am assuming that Book model has fiction_id as well because of the has_many association shown in this line Fiction.find(params[:ID]).books. There could be two approaches achieve this. First one could be that you use #filtered_books variable and extract users from it like #filtered_books.collect {|b| b.users}.flatten to extract all the users. Second approach could be through associations using fiction_id which could be something like User.joins(:books).where(books: {id: #filtererd_books.pluck(:id)})
So I have an app in which users can create cars. They can also like cars and I want to create an association between both. Cars that they created belong to them and Cars that they have liked belong to them through the context of liking them. To do this I have set up my associations as follows:
User Association:
class User < ActiveRecord::Base
has_many :cars
has_many :cars, -> {distinct}, through: :likes
end
Car Association:
class Car < ActiveRecord::Base
belongs_to :users
has_many :likes
has_many :users, -> { distinct }, through: :likes
end
Like Association:
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :car
end
The problem is that before I had my user has_many cars through like relationship declared. I used to be able to call #user.cars and it would present the user's cars. Now it returns the collection of the cars the user has liked. I need methods for each collection.
When I try: User.likes.cars
I get a
No Method error
and the console log looks through the likes records and still doesn't return the cars even though my likes records have a car_id field.
I have looked at a bunch of questions but have trouble understanding them. I've also tried to define methods in the model and nothing is seeming to work. Any help is appreciated.
How would I be able to change my associations so I can have a for query both User.cars (for cars the user has created) and User.likes.cars (for cars the user has liked)?
So the below answer from Oleg didn't work exactly but led me in the right direction. Thank you! I started by following the above example and doing:
class User < ActiveRecord::Base
has_many :cars
has_many :car_likes, -> {distinct}, class_name: 'Car', through: :likes
end
class Car < ActiveRecord::Base
belongs_to :users
has_many :likes
has_many :user_likes, -> { distinct }, class_name: 'User', through: :likes
end
This returned the following error in console:
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) "car_likes" or :car_like in model Like. Try 'has_many :car_likes, :through => :likes, :source => '. Is it one of user or car?
So I changed it to:
class User < ActiveRecord::Base
has_many :cars
has_many :car_likes, -> {distinct}, through: :likes, source: :cars
end
Car Association:
class Car < ActiveRecord::Base
belongs_to :users
has_many :likes
has_many :user_likes, -> { distinct }, through: :likes, source: :users
end
It nows works for both models! Thanks and hopefully this is helpful to someone else with the same problem.
has_many :cars, -> {distinct}, through: :likes overrides has_many :cars because it redefines User.cars. Try the following:
class User < ActiveRecord::Base
has_many :cars
has_many :car_likes, -> {distinct}, class_name: 'Car', through: :likes
end
Car Association:
class Car < ActiveRecord::Base
belongs_to :users
has_many :likes
has_many :user_likes, -> { distinct }, class_name: 'User', through: :likes
end
#To get them, instead of user.likes.cars
#user.car_likes
#car.user_likes
Please let me know if the problem persists. There might be another error.
I don't see where you are defining any model as polymorphic.
In the past I have done something like this.. actually I did this for tags/taggings and made "like" a tag a user applied to another instance. This is an ad-hoc modification and I may have missed something but it's a pretty common use case for polymorphic associations.
class Like < ActiveRecord::Base
belongs_to :likeable, polymorphic: true
...
end
class Liking < ActiveRecord::Base
belongs_to :like
belongs_to :likeable, :polymorphic => true
end
class User < ActiveRecord::Base
has_many :likings, :as => :likeable
has_many :likes, -> { order(created_at: :desc) }, :through => :taggings
end
I have the following associations:
class Venue < ActiveRecord::Base
has_many :sales
end
class Sale < ActiveRecord::Base
has_many :sale_lines
has_many :beverages, through: :sale_lines
end
class SaleLine < ActiveRecord::Base
belongs_to :sale
belongs_to :beverage
end
class Beverage < ActiveRecord::Base
has_many :sale_lines
has_many :sales, through: :sale_lines
has_many :recipes
has_many :products, through: :recipes
end
class Recipe < ActiveRecord::Base
belongs_to :beverage
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :recipes
has_many :beverages, through: :recipes
end
I wan't to see the quantity of products sold by each venue, so basically I have to multiply the recipe.quantity by the sale_line.quantity of an specific product.
I would like to call #venue.calc_sales(product) to get the quantity sold of product.
Inside the class Venue I am trying to calculating it by:
class Venue < ActiveRecord::Base
has_many :sales
def calc_sales(product)
sales.joins(:sale_lines, :beverages, :recipes).where('recipes.product_id = ?', product.id).sum('sale_lines.quantity * recipe.quantity')
end
end
However, I can't access the recipes in that way.
Any idea on how to achieve it?
For the joins, you have to use a Hash to join a already-joined table. It's hard to explain, but here are some examples:
Venue.joins(:sales, :beverages) : This implies that the relations :sales and :beverages are declared on the Venue model.
Venue.joins(:sales => :beverages) : This implies that the relation :sales exists on the Venue model, and the relation :beverages exists on the Sale model.
Consider this:
Venue
has_one :sale
Venue.joins(:sales) : This would not work, you have to use the exact same name as the relation between the Venue model & Sale model.
Venue.joins(:sale) : This would work because you used the same name of the relation.
Attention: You have to use the pluralized name in the where clause:
Venue.joins(:sale).where(:sales => { :id => sale.id })
^^ ^^ # See the plural
In your case, you can do something like this:
sales.joins(:sale_lines => { :beverage => :recipes })
.where(:recipes => { :product_id => product.id })
.sum('sale_lines.quantity * recipes.quantity')
I have this relationship between categories, products & brands:
class Brand < ActiveRecord::Base
has_many :products
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
belongs_to :brand
end
How can I select all categories by specified brand with this relations?
I try this but get an error
b = Brand.find(1)
Category.joins(:products).where(:products => b.products)
You did the right thing with the join, just add a more complex where definition:
Category.joins(:products).where(:products => {:brand_id => 1})
Controversially HABTM's are rarely, if ever, a good design and IMO just about the only thing Rails got wrong.
Introduce an xref table to join products and categories and use has_many :through on both sides of the relationship so you end up with
class Brand < ActiveRecord::Base
has_many :products
has_many categories :through => products # This is now allowed in Rails 3.x and above
end
class Category < ActiveRecord::Base
belongs_to :product_category
has_many :products :through => product_category
end
class Product < ActiveRecord::Base
belongs_to :brand
belongs_to :product_category
has_many :categories :through => product_category
end
class ProductCategory < ActiveRecord::Base
has_many :products
has_many :categories
end
This gives you the best flexibility with the least amount of code re-factoring for you plus a much more intuitive path to get whatever data you need on either side of the relationship and will enable you to achieve the following
b = Brand.find(1)
b.categories.all
Update
The above is totally untested code and I have just corrected a glaringly stupid mistake I made. If you have any issues implementing this then come back
The image shows part of my data model. I would like to fetch all items that are associated with a user (through organizations and items_group). How should I change the models and write this query in the controller? Using :through => organizations I can get all items_groups but I don't how to include one more relation to query related items.
class User < ActiveRecord::Base
has_and_belongs_to_many :organizations
has_many :items_groups, :through => :organizations
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :items_groups
has_many :items, :through => :items_groups
end
class ItemsGroup < ActiveRecord::Base
has_many :items, :inverse_of => :items_group
has_and_belongs_to_many :organizations
has_many :users, :through => :organizations
end
I think you might have to do it back-to-front and find items joined back to your user.
You could define method like this in your User class:
def items
Item.joins(:items_group => {:organizations => :users}).where("users.id" => self.id).select("distinct items.*")
end
The items it returns will be read-only because of the explicit select but I think you'll want that to avoid returning individual items more than once.
If you set in your models the relationships this should work:
users.organizations.item_groups.items
Though for it to work your models should contain this:
class User < ActiveRecord::Base
has_many :organizations, :through => :organization_users
end
class Organization < ActiveRecord::Base
has_many :item_groups, :through => :items_groups_organizations
end
class ItemsGroup < ActiveRecord::Base
belongs_to :item
end
Hope it works for you!