How to implement the data structure for "transferring ownership" of a model

Chris Yeung

I am using rails to build an application where an owner owns many products. And each product can be transferred to another owner when needed.

At my first thought I imagine a simple relationship

Owner has_many :products product belongs_to owner

As I need to add a many_to_many relationship, I thought may be I can add a owners_products_table. However I will not be able to distinguish the period of when each owner owns the product.

I thought have adding columns like start_owning_at, and end_owning_at ..but it seems to make all the query process very troublesome..

I wonder how I can implement the transfer ownership data relationship?

Max Williams

So, you need to keep track of the period for which each user owns a product? I think your instinct about how to model it is correct, and you can work on making the queries simple and intuitive.

I would model this like so:

Owner 
  has_many :product_ownerships
  has_many :products, :through => :product_ownerships

Product 
  has_many :product_ownerships
  has_many :owners, :through => :product_ownerships
  #some named scopes for convenience
  scope :at_time, ->(time) { where("product_ownerships.ownership_starts_at <= ? and product_ownerships.ownership_ends_at => ?", time, time)} 
  scope :current, -> { at_time(Time.now) }  

ProductOwnership
  belongs_to :owner
  belongs_to :product
  #fields: product_id, owner_id, ownership_starts_at, ownership_ends_at
  #some named scopes for convenience
  scope :at_time, ->(time) { where("product_ownerships.ownership_starts_at <= ? and product_ownerships.ownership_ends_at => ?", time, time)} 
  scope :current, -> { at_time(Time.now) }

Now you should be able to say things like

  @owner = Owner.find_by_id(params[:id])
  @products = @owner.products.current
  #or 
  @products = @owner.products.at_time(Time.parse(params[:time]))

etc, or do the same to list product_ownerships rather than products: this is useful if you have a form page where the user can update the times of the product_ownerships, for example.

EDIT - btw, in this schema, when a new Owner takes a product, you should make a new ProductOwnership, and set the ownership_ends_at field for the old owner to be the handover time.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Transferring ownership to a manager

From Dev

Thoughts on how to structure data model

From Dev

Hibernate how to implement dynamic data structure

From Dev

How to implement a Set Data Structure in Java?

From Dev

How to implement data validation in model of MVVM?

From Dev

How can I model the following data structure?

From Dev

How to structure data model for a correct deserialisation?

From Dev

Which data structure will be suitable for storing three related values? And how to implement?

From Dev

How to implement my own LinkedList<LinkedList> data structure in Java?

From Dev

How implement data structure which is map many to one?

From Dev

how to implement below data structure in java using collection framework

From Dev

how to implement below data structure in java using collection framework

From Dev

How to implement Java graph data-structure and class with restricted visibility?

From Dev

Transferring ownership of an IDisposable object and the builder design pattern

From Dev

Transferring Ownership in vector of unique_ptrs

From Dev

Transferring ownership of an IDisposable object and the builder design pattern

From Dev

Suggest Data Structure which implement this

From Dev

how to implement a Model for these needs

From Dev

How to validate response data against Guzzle model structure?

From Dev

How to validate response data against Guzzle model structure?

From Dev

What fundamental data structure is used to implement NSOrderedSet

From Dev

What is the best data structure to implement a queue?

From Dev

Which data structure is most suitable to implement a Dictionary?

From Dev

Best data structure to implement the APIs for Game Scoreboard

From Java

Why can I still use a variable after transferring ownership of it into a function?

From Dev

Correct Flattened Data Model and noSQL data structure

From Dev

Correct Flattened Data Model and noSQL data structure

From Dev

How to implement a Model in Spring MVC?

From Dev

how to implement abstract model in spyne

Related Related

  1. 1

    Transferring ownership to a manager

  2. 2

    Thoughts on how to structure data model

  3. 3

    Hibernate how to implement dynamic data structure

  4. 4

    How to implement a Set Data Structure in Java?

  5. 5

    How to implement data validation in model of MVVM?

  6. 6

    How can I model the following data structure?

  7. 7

    How to structure data model for a correct deserialisation?

  8. 8

    Which data structure will be suitable for storing three related values? And how to implement?

  9. 9

    How to implement my own LinkedList<LinkedList> data structure in Java?

  10. 10

    How implement data structure which is map many to one?

  11. 11

    how to implement below data structure in java using collection framework

  12. 12

    how to implement below data structure in java using collection framework

  13. 13

    How to implement Java graph data-structure and class with restricted visibility?

  14. 14

    Transferring ownership of an IDisposable object and the builder design pattern

  15. 15

    Transferring Ownership in vector of unique_ptrs

  16. 16

    Transferring ownership of an IDisposable object and the builder design pattern

  17. 17

    Suggest Data Structure which implement this

  18. 18

    how to implement a Model for these needs

  19. 19

    How to validate response data against Guzzle model structure?

  20. 20

    How to validate response data against Guzzle model structure?

  21. 21

    What fundamental data structure is used to implement NSOrderedSet

  22. 22

    What is the best data structure to implement a queue?

  23. 23

    Which data structure is most suitable to implement a Dictionary?

  24. 24

    Best data structure to implement the APIs for Game Scoreboard

  25. 25

    Why can I still use a variable after transferring ownership of it into a function?

  26. 26

    Correct Flattened Data Model and noSQL data structure

  27. 27

    Correct Flattened Data Model and noSQL data structure

  28. 28

    How to implement a Model in Spring MVC?

  29. 29

    how to implement abstract model in spyne

HotTag

Archive