I currently have a ProductSale model that has_many sales.
Also a sale belongs to an invoice.
My goal is to access an invoice through a ProductSale's association to sales. (product_sale.invoice)
Current ProductSale model below:
class ProductSale < ApplicationRecord
has_many :sales
has_one :invoice, through: :sales
end
However my current error is saying that this can't be done because the :through association is a collection, which i understand. Is there a way that this can be possible?
class Sale < ApplicationRecord
belongs_to :invoice
end
class Invoice < ApplicationRecord
has_many :sales, inverse_of: :invoice, dependent: :destroy
end
All the sales on the ProductSale object have the same invoice. You know that you can just use the invoice of the first sale, but associations won't know that all the sales have the same invoice so you can use any, for instance the first one.
To have a invoices method to get each of the invoices you could do this:
class ProductSale < ApplicationRecord
has_many :sales
has_many :invoices, through: :sales
end
However, if you want to use your business logic that all the invoices are assumed to be the same, you will have to write a method to implement that business logic.
class ProductSale < ApplicationRecord
has_many :sales
def invoice
sales.first&.invoice
end
end
If all the sales on the invoice will be sales in the ProductSale object, then maybe you should refactor as below.
class ProductSale < ApplicationRecord
has_one :invoice
delegate :sales, :to => :invoice, :prefix => false, :allow_nil => true
end
Then you can both call the invoice method to get the invoice and also call the sales method to get all the sales on the invoice for the ProductSale object.
How about:
class ProductSale < ApplicationRecord
has_many :sales
has_one :sale
has_one :invoice, through: :sale
end
Related
I want a user to be able to create and save or choose an existing client to add to an invoice. There can only be one client per invoice.
I currently have 3 models users invoices and items
I am using a simple has_many relationship currently but I am getting confused now that I want to add a new table clients. I was hoping I could get some advice on what association to use.
My current associations
class User < ActiveRecord::Base
has_many :invoices
class Invoice < ActiveRecord::Base
belongs_to :user
has_many :items, :dependent => :destroy
class Item < ActiveRecord::Base
belongs_to :invoice
So I was thinking to do something simple like adding has_many :clients
to users, add has_one :client to invoices and add the table clients
class User < ActiveRecord::Base
has_many :invoices
has_many :clients
class Client < ActiveRecord::Base
belongs_to : user
class Invoice < ActiveRecord::Base
belongs_to :user
has_many :items, :dependent => :destroy
has_one :client
Would this work? Is there a better way?
It is very rare that you will use has_one. In your case the following models make more sense:
class User < ActiveRecord::Base
has_many :invoices
end
class Invoice < ActiveRecord::Base
belongs_to :user
belongs_to :client
has_many :items, :dependent => :destroy
end
class Item < ActiveRecord::Base
belongs_to :invoice
end
class Client < ActiveRecord::Base
has_many :invoices
has_many :items, through: :invoices
end
Here is my models:
class User < ActiveRecord::Base
has_many :products
has_many :comments
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
I need to get comment records from current user products only
How do I do that? thanks
If we move the relationships to use a has_many: comments, through: products you can probably get what you're after:
class User < ActiveRecord::Base
has_many :products
has_many :comments, through: products
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
Now you can do user.comments.
The rails docs are here, which say:
A has_many :through association is often used to set up a many-to-many
connection with another model. This association indicates that the
declaring model can be matched with zero or more instances of another
model by proceeding through a third model. For example, consider a
medical practice where patients make appointments to see physicians.
I have these models in my Rails 4 app:
class Invoice < ActiveRecord::Base
has_many :allocations
has_many :payments, :through => :allocations
end
class Allocation < ActiveRecord::Base
belongs_to :invoice
belongs_to :payment
end
class Payment < ActiveRecord::Base
has_many :allocations
has_many :invoices, :through => :allocations
end
How can I get all the invoices that are related to one particular payment X?
I've been trying to get my head around this all day but to no avail.
Thanks for any help.
As simple as:
payment.invoices
This will follow your association, passing through the allocation model.
Try this,
Identify the Payment and then its invoices like this
payment_invoices = Payment.find(payment_id).invoices
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 5 classes that are part of a point of sales program:
Customer, Technician, Order, Ticket, Service
Each Order has one customer and many tickets. Each Ticket has on technician and many services.
How should I make the associations so that I can do the following lookup:
customer.order.find_by_date(10/20/2010).service.technician.name
This is what I am planning:
class Customer < ActiveRecord::Base
has_many :orders
has_many :tickets, :through => :orders
has_many :technicians, :through => :tickets
end
class Technician < ActiveRecord::Base
belongs_to :ticket
belongs_to :service
end
class Service < ActiveRecord::Base
has_many :technicians
belongs_to :ticket
end
class Ticket < ActiveRecord::Base
has_many :services
has_many :technicians, :through => :services
end
class Order < ActiveRecord::Base
has_many :tickets
has_many :services, :through => tickets
has_many :technicians, :through => services
belongs_to :customer
end
The relationship between customer and order is one-to-many. And the relationship between order and service is also one-to-many. Therefore you cannot do:
customer.order.find_by_date('10/20/2010').service.technician.name
It should be:
customer.orders.find_by_date('10/20/2010').services.each do |service|
service.technician.name
end
It's a similar problem as in your other question.