Rails newbie here so please dont bang your head too much when you see what I'm having issues with (It should probably be straightforward!)
I'm working on an app whereby clients have services based on products (VPS) for sale. Whilst the number of actual products are minimal, they are highly configurable and pricing is dependant on a few factors:
Ram / HDD Size
The Location of the server (which may also change the currency)
Any product 'add-ons'.
I'm having an issue with how I'd actually go about implementing pricing this (generating a price from the various factors to add to a users service) - and after speaking to people they've given me different answers which has left me even more confused.
If it helps, I have my models like so:
class Product < ActiveRecord::Base
has_many :services
has_many :prices
has_many :addons
belongs_to :product_cat
end
class Service < ActiveRecord::Base
belongs_to :product
belongs_to :user
belongs_to :location
end
class Location < ActiveRecord::Base
has_one :currency
has_many :services
end
Any help would be appreciated.
Cheers.
First of all, I think you should take some time to think a bit more about your database structure.
What type of relation do you have between Products and Services?
From what I understand, a product can have several services, and a service can be active for several products. This sound like a n->m association to me.
If it's like that, you should have your tables set-up like this:
class Product < ActiveRecord::Base
has_many :product_services
has_many :services, through: :product_services
has_many :prices
has_many :addons
belongs_to :product_cat
end
class ProductServices < ActiveRecord::Base
belongs_to :product
belongs_to :service
end
class Service < ActiveRecord::Base
has_many :product_services
has_many :products, through: :product_services
belongs_to :user
belongs_to :location
end
class Location < ActiveRecord::Base
has_one :currency
has_many :services
end
With this setup, getting the price for a product should be pretty straightforward.
If your product has a base price, and all of your services have a price, then the total price of the product is the sum of the base price and of all the individual services prices.
Now that you have your associations in place, you can access them with the following:
product.services # returns all of the services for the specific product.
product.services.sum(:price) # returns the sum of all the individual prices of each service.
I hope this will help you in implementing your pricing solution. It can be a complex problem, but Rails provides a number of way to make it easier. As a side note, you should really have a look at the active_record documentation, it's full of information that will make your life easier.
Cheers!
Related
I have the following schema for my Rails app :
A project has many reviews, each of those reviews is filled out by a unique User to calculate a global score.
We could say that "An Organization of Users handles many Entities that have many Projects which are reviewed by the Users".
As you can see, I have a circular reference since I linked the tables "Users" & "Reviews".
After many tries and many search, I just can't manage to associate a User to a Review...Here is what I did so far:
1. Creation of an association table "UserReviews"
2. Model User
class User < ApplicationRecord
belongs_to :organization
####
has_many :user_reviews
has_many :reviews, through: :user_reviews
end
3. Model Review
class Review < ApplicationRecord
belongs_to :project
##
belongs_to :user_reviews
has_one :user, through: :user_reviews
end
4. Model UserReview
class UserReview < ApplicationRecord
belongs_to :user
belongs_to :review
end
I want to be able to get user.reviews or review.user.
Basically...I have to admit I'm lost despite the documentation. I never had to deal with this kind of issue.
Many thanks for your help!
Why do you need UserReview model here? I suppose Review model suffices your use case.
Change the Review model to:
class Review < ApplicationRecord
belongs_to :project
belongs_to :user
end
I'm uncertain of the best approach to model conversation relationships.
My application has Users, Conversations, UserConversations, Venues, & VenueConversations.
Users can have many Conversations & Conversations can have many Users. Thus, the bridging table (UserConversations).
Venues can also have Conversations and these Conversations can also have many Users. Thus, the bridging table (VenueConversations)
I've tried it with polymorphic associations but then Messages in the Conversations became difficult to manage. Also, a while back I asked a question on Stack and someone said I should steer clear of polymorphic associations at all costs.
I hope to do this properly so that querying doesn't become a nightmare in the future.
I'd also like to state I'd like to have one Conversation model because one day they'll share a lot of behavior.
User.rb
class User < ApplicationRecord
has_many :user_conversations
has_many :conversations, through: :user_conversations
has_many :venue_conversations
has_many :conversations, through: :venue_conversations
end
UserConversation.rb
class UserConversation < ApplicationRecord
belongs_to :user
belongs_to :conversation
end
Venue.rb
class Venue < ApplicationRecord
has_many :venue_conversations
has_many :conversations, through: :venue_conversations
end
VenueConversations.rb
class VenueConversations < ApplicationRecord
belongs_to :user
belongs_to :venue
belongs_to :conversation
end
The problem I'm having now is that when I do User.first.conversations I only find one of the types of conversations a User could have(Private vs Venue). It reflects the last definition I gave to has_many :conversations in the User.rb model. Perhaps a scope is in order? How would I make a distinction between private & venue conversations as well?
Please advise me on how I could resolve this or take a better approach
I'm using rails 4 and i have a possible three way many-to-many relationship situation that looks like this: To register a sale, i need a Client and a Product, along with its quantity. The threeway comes when i need a fixed id for the Sale because of another table Installments, with due dates and values for each installment and to check if they are paid or not.
I got a little confused with the relationships between them, but i did something like this (i called the middle table i used here as SCP, sale_client_product):
class Client < ActiveRecord::Base
has_many :scps
end
class Sale < ActiveRecord::Base
has_many :scps
has_many :installments
end
class Installment < ActiveRecord::Base
belongs_to :sale
end
class Product < ActiveRecord::Base
has_many :scps
end
class SCP < ActiveRecord::Base
belongs_to :client
belongs_to :product
belongs_to :sale
end
I would like to know if this is correct or if there is a better way of doing it. I'm also in a picle when actually saving, as for any one sale i may have to save many SCP instances with one form.
If you want to have Product, Client, Sale, Installment information in a single document, maybe you can use the Invoice.
In business, if a company want to ask money from a customer, he will periodically send him an invoice which includes all the following infos:
Product details, names, quantity, price for each unit
Client details, names, address, phone number
Sale details, total price, date of sale or Installment due date, installment amount
Class Client
has_many :invoices
end
Class Product
has_many :invoices
end
Class Sale
has_many :installments
has_many :invoices, through: :installments
end
Class Installment
belongs_to :sale
end
Class Invoice
belongs_to :client
belongs_to :product
belongs_to :installment
end
Payment will be what you need to reconcile with Invoice, so that you can understand which Invoices (and consequently Installments) were paid.
Class Payment
belongs_to :invoice
end
when you do #invoice.payment you get the payment for that invoice (which belongs_to :installment), if #invoice.payment is nil, then the installment was not paid.
If you want to register a Payment it should have an existing Invoice
#payment = Payment.new(payment_params)
#payment.invoice_id = #invoice.id
#payment.save
This is kind of how I would register Accounting Transactions when I was working in the Accounting Field. Feel free to improve your app and maybe in the future we can edit this post for improvements
A possible improvement could be enstablishing that Installment has_one :payment, through: :invoice, so you figure that out!
Still a newbie here, but I still couldn't get the logic right.
Currently, I have:
User has many products.
Product has 1 user with a price attribute.
I am trying to add on:
User can offer 1 price on a product sold by another user. User can offer price on multiple products.
A Product can have many offered price by multiple users.
I have currently come out with:
class User < ActiveRecord::Base
has_many :products
has_many :offered_prices
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :offered_prices
end
This is what I have done so far. It still doesn't seem quite right as I am rather confused at the same time. Your help is very much appreciated! :)
Define three models:
User | OfferedPrice | Product
The association amongst them will be:
class User < ActiveRecord::Base
has_many :products
has_many :offered_prices, through: :products
end
class OfferedPrice < ActiveRecord::Base
belongs_to :user
belongs_to :product
# To make sure a user can offer price once for against a product
validates_uniqueness_of :price, scope: [:user, :product]
end
class Product < ActiveRecord::Base
has_many :offered_prices
has_many :user, through: :offered_prices
end
I have a problem and I have a bit of a hard time explaining it. Here are my models:
class Company < ActiveRecord::Base
has_one :publisher
has_one :developer
end
class Publisher < ActiveRecord::Base
belongs_to :company
has_many :games
end
class Developer < ActiveRecord::Base
belongs_to :company
has_many :games
end
class Game < ActiveRecord::Base
belongs_to :publisher
belongs_to :developer
end
Basically a Company can be a Developer or Publisher or both. Then I can call Company.publisher.games to see all the games the company published, and Company.developer.games to see the games they developed.
The problem is I don't know the most efficient way to nest the forms for this.
Basically when I am at /company/new, I want to have a form for the company, which in turn has a box where I can specify multiple game IDs for either the Publisher or the Developer aspect of the company. I can't seem to figure out how I can just do #company.update_attributes(params[:company]) and have that take a nested form and create a publisher and or developer with multiple associated games.