Find records have count of association > n - ruby

class PurchaseOrder < ApplicationRecord
has_many :purchase_order_materials
end
class PurchaseOrderMaterial < ApplicationRecord
belongs_to :purchase_order
belongs_to :material
end
I have this query:
PurchaseOrderMaterial
.includes(:purchase_order, :material)
.where("purchase_orders.provider_id IS NULL AND purchase_orders.status = ?
AND (purchase_orders.is_waiting != ?
OR purchase_orders.is_waiting IS NULL)",
PurchaseOrder.possibleStatus.first, true)
All i want it's filter results to purchase_order.purchase_order_materials.count > 1.
Some way to achieve it?

Related

Best way to count ActiveRecord child custom properties?

If I have a shopping cart, and in it is produce (where each produce has a custom expired property), what is the best way to count the expired property? -- is there a better way than this count_expired?
class Cart < ActiveRecord::Base
has_many :produce, :dependent => :delete_all
def count_expired
count = 0
self.produces.each do | produce |
if produce.expired
count ++
end
end
return count
end
end
class Produce < ActiveRecord::Base
belongs_to :cart, class_name: "Cart", touch: true
def expired
return <true or false logic>
end
end

Ruby - How to find Customer with nested join

I have the following relationships:
class Location < ApplicationRecord
has_many :weddings
class Wedding < ApplicationRecord
has_many :wedding_memberships
class WeddingMembership < ApplicationRecord
belongs_to :wedding
belongs_to :customer
class Customer < ApplicationRecord
has_many :wedding_memberships
And what I would like to find is the Customers in Location with id: 1
My attempts WeddingMembership.joins(:wedding).where(wedding: {location_id: 1}) are giving me the following errors: Caused by PG::UndefinedTable: ERROR: missing FROM-clause entry for table "wedding"
Try this one, it's a multiple join with a where clause on your location_id
Customer.joins(wedding_memberships: :wedding)
.where(weddings: { location_id: 1 })
.distinct

Query an ActiveRecord on several tables?

Sorry to ask this question but I'm really newbie with Ruby and I need help to update several records on my database.
I'm using ActiveRecord to query the database. Let say I have a table Product that contains SubProduct that also contains SubSubProduct. Now I would like to write a simple query to get all SubSubProduct of Product.
To get a list of SubSubProduct I usually do this
ssp = SubSubProduct.where(sub_sub_type: "example")
Now to use a where clause on relational element how can I do
ssp = SubSubProduct.where(sub_sub_type: "example", SubProduct.Product.type: "sample")
Set up ActiveRecord associations in your models:
#app/models/product.rb:
class Product < ActiveRecord::Base
has_many :sub_products
end
#app/models/sub_product.rb:
class SubProduct < ActiveRecord::Base
belongs_to :product
end
#app/models/sub_sub_product.rb:
class SubSubProduct < ActiveRecord::Base
belongs_to :sub_product
end
What you wanted:
ssp = SubSubProduct.where(sub_sub_type: "example", SubProduct.Product.my_type: "sample")
The correct syntax:
ssp = SubSubProduct.includes(sub_product: :product).where(sub_sub_type:"example", products: {my_type: "toto"})
This is performed via associations.
class SubSubProduct
has_many :products
end
Then you can do things like
sub_product.products
and it will product all the products associated with them.
Try a nested includes:
`Product.includes(subproducts: :subsubproducts)'
For this, you'll want to set up ActiveRecord associations in your models:
In product.rb:
class Product < ActiveRecord::Base
has_many :sub_products
has_many :sub_sub_products, through: :sub_products
end
In sub_product.rb:
class SubProduct < ActiveRecord::Base
belongs_to :product
has_many :sub_sub_products
end
In sub_sub_product.rb:
class SubSubProduct < ActiveRecord::Base
belongs_to :sub_product
end
Then, if you have a Product and want its SubSubProducts, you can use:
# p is a Product object
p.sub_sub_products

merge ActiveRecord::Relation with ActiveRecord::Associations

I have 2 models
class User < ActiveRecord::Base
has_many :games
def created_games
Game.where(created_by: self.id)
end
end
class Game < ActiveRecord::Base
belongs_to :user
end
u = User.take
joined_games = u.games
created_games = u.created_games
the variable joined_games is a instance of ActiveRecord::Associations, and created_games is an instance of ActiveRecord::Relation.
Is there any way I can join joined_games and created_games together?
Try adding a scope in User model
scope :joined_games, where(user_id: self.id, created_by: self.id)

ActiveRecord - Finding all objects with shared attributes in a join model

I have three models
class Boat < ActiveRecord::Base
belongs_to :captain
has_many :boat_classifications
has_many :classifications, through: :boat_classifications
end
class Classification < ActiveRecord::Base
has_many :boat_classifications
has_many :boats, through: :boat_classifications
end
class BoatClassification < ActiveRecord::Base
belongs_to :boat
belongs_to :classification
end
I'm trying to write a simple ActiveRecord query to find all the boats of type sailboat. Something like Boat.where(classifications: "Sailboat")
I think this could work:
Boat.joins(:classifications).where(classifications: { name: 'Sailboat' }) # name or whatever field contains Sailboat
Generates this query:
SELECT `boats`.* FROM `boats` INNER JOIN `boat_classifications` ON `boat_classifications`.`boat_id` = `boats`.`id` INNER JOIN `classifications` ON `classifications`.`id` = `boat_classifications`.`classification_id` WHERE `classification`.`name` = 'Sailboat'
I think you want something like this:
Boat.includes(:classifications).where(classifications: {id: Classification.sailboats})
For this to work, you also need a scope on Classification like this:
def self.sailboats
where(name: "Sailboat")
end

Resources