0

My rails application has a Rates model, which consists of many different table/references for each column. The only real variable is my Cost column (i input a dollar amount) and the default created_at and updated_at columns.

In my Rates index, currently i show all of the currently existing rates, model and schema below:

Rates Table      

create_table "rates", force: :cascade do |t|
    t.integer  "origin_id"
    t.integer  "destination_id"
    t.integer  "carrier_id"
    t.integer  "shipment_category_id"
    t.decimal  "cost",                 precision: 10, scale: 2
    t.integer  "unit_of_measure_id"
    t.datetime "created_at",                                    null: false
    t.datetime "updated_at",                                    null: false
    t.integer  "seller_id"
    t.index ["carrier_id"], name: "index_rates_on_carrier_id", using: :btree
    t.index ["destination_id"], name: "index_rates_on_destination_id", using: :btree
    t.index ["origin_id"], name: "index_rates_on_origin_id", using: :btree
    t.index ["seller_id"], name: "index_rates_on_seller_id", using: :btree
    t.index ["shipment_category_id"], name: "index_rates_on_shipment_category_id", using: :btree
    t.index ["unit_of_measure_id"], name: "index_rates_on_unit_of_measure_id", using: :btree

Rates Model
  belongs_to :origin, :class_name => 'Port'
  belongs_to :destination, :class_name => 'Port'
  belongs_to :seller, :class_name => 'Carrier'
  belongs_to :carrier
  belongs_to :shipment_category
  belongs_to :unit_of_measure

I was going to create a migration using this solution i found ( https://stackoverflow.com/a/34425284/7631715).

The thing is, i don't want this to stop me from importing "duplicate" data later. Since the rates from different Sellers (seller_id) change over time, i wanted to keep that data, so later i could make a line chart to see the cost variation over time.

Essentially my end product i want my page, (index) just to show the rates that are not duplicates, and if there are duplicated, only show the lasted added / created rates (last created, would be the current valid rate.)

Do i even bother creating that migration to add the uniqueness / constraint? Or should i just do this at the controller level. If controller, how would i go about that?

Thanks,

Community
  • 1
  • 1
vinicomelo
  • 53
  • 4
  • It's not clear what you are asking. I understand that you are considering a uniqueness constraint on a single field or together with another field. And I see that you are interested in whether to add the constraint at the database level or in an ActiveRecord validation. Beyond that I'm not able to follow. What is the goal? – moveson Mar 21 '17 at 16:17
  • Sorry, so my goal is i want to add a lot of rates. But, on my index/front page, i only want to show the most recent / active (most current created_at) if the origin, seller, carrier, cost, etc are all the same. But let's say, i add a new rate, that has all the same values, except for cost (cheaper for example), i only want to display that new cheaper rate, because it was the last one added, therefore the current valid / usable rate. – vinicomelo Mar 21 '17 at 16:33
  • By rates, I assume you refer to the `cost` field. If that is the case, then you can't have a uniqueness constraint on the `cost` field alone, either at the database or the ActiveRecord level. On your index page, you'll want to filter and sort by `created_at` choosing only the most recent record. If you want to ask about how to do that filter and sort, you'll want to ask a new question. – moveson Mar 22 '17 at 14:42

0 Answers0