You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classItemOwnership < ActiveRecord::Base# This class models the ownership relationship between items and customers# For example, you'd have just one entry in the "items" table for an# iPhone 6, and a separate row in the "item_ownerships" table for every# customer who owns one.# The "items" table defines the various types of items, and the "item_ownerships"# table records each unique customer/item combination by referencing the item# type id from the "items" table and the customer id from the "customers"# table.belongs_to:itembelongs_to:customer# Require both a customer and item type to be specifiedvalidates:customer,presence: truevalidates:item,presence: trueend
app/models/ticket.rb
classTicket < ActiveRecord::Basebelongs_to:customerhas_and_belongs_to_many:item_ownershipshas_and_belongs_to_many:service_taskshas_many:comments,as: :commentable# Can't create ticket without both customer and description.# Associated items are optional.validates:description,presence: truevalidates:customer,presence: truedeftotal_services_priceself.service_tasks.map(&:price).reduce(:+)enddeftotal_parts_priceself.service_tasks.map(&:total_parts_price).reduce(:+)enddeftotal_parts_costself.service_tasks.map(&:total_parts_cost).reduce(:+)endend
classCreateInitialTables < ActiveRecord::Migrationdefchangecreate_table:customersdo |t|
t.timestampst.string:name# add any other fields you want for customers hereendcreate_table:itemsdo |t|
t.timestampst.string:name# add any other fields you want hereendcreate_table:item_ownershipsdo |t|
t.timestampst.references:customer,index: truet.references:item,index: truet.timestampsendcreate_table:ticketsdo |t|
t.timestampst.string:descriptiont.references:customer,index: trueendcreate_join_table:items,:ticketsdo |t|
t.index:item_idt.index:ticket_idendcreate_table:service_tasksdo |t|
t.timestampst.string:descriptiont.decimal:price,precision: 8,scale: 2endcreate_join_table:service_tasks,:ticketsdo |t|
t.index:service_task_idt.index:ticket_idendcreate_table:partsdo |t|
t.timestampst.string:namet.decimal:price,precision: 8,scale: 2t.decimal:cost,precision: 8,scale: 2endcreate_table:part_requirementsdo |t|
t.timestampst.integer:quantityt.references:partt.references:service_taskendcreate_table:commentsdo |t|
t.timestampst.references:commentable,polymorphic: true,index: truet.text:textendendend
Usage
Run Database Migrations
$ rake db:migrate
Rails console for tinkering
$ rails console
# Lets add an iPhone 6 to the items tableiphone6=Item.create(name: "iPhone 6")# Now we'll create a service task definition for replacing an iPhone 6 screenreplaceScreen=ServiceTask.create(description: "Replace iPhone 6 Screen",price: 30.00)# We can associate the parts needed (the actual screen) with the service task.# First, create the part:iphone6Screen=Part.create(name: "iPhone 6 Screen",cost: 30.00,price: 65.00)# Then add a new PartRequirement to the ServiceTask we just created:replaceScreen.part_requirements.create(part: iphone6Screen,quantity: 1)# Oh look, our first customer!brian=Customer.create(name: "Brian McKelvey")# And he has an iPhone 6.brian.items << Item.find_by_name("iPhone 6")# Now we can create a ticket for the broken screen:ticket=Ticket.create(customer: brian,description: 'Needs screen replaced.')# ...and add the "Replace iPhone 6 Screen" task to the ticket:ticket.service_tasks << replaceScreen# ...and add not just any old iPhone 6, but Brian's iPhone 6:ticket.item_ownerships << brian.item_ownerships.last# Because the "iPhone 6 Screen" part is linked to the "Replace iPhone 6 Screen"# service task, which is linked to the customer's ticket, we can now calculate# how much the parts will cost in addition to the labor cost.puts"Price breakdown:"puts"Labor: $ #{sprintf("%.2f",ticket.total_services_price)}"puts"Parts: $ #{sprintf("%.2f",ticket.total_parts_price)}"