ruby - What is the correct Rails association for this design? -


environment: ruby 2.0.0, rails 4.1, windows 8.1, devise

i confused how handle issue. have items stored @ location until checked out user. want able query item , determine location or user holds it. so, item belong either location or user. each location , each user can have many items.

this large library system book in 1 of several buildings , patron check out books each of buildings. @ point, i'd locate book either in building or patron.

my confusion stems fact item associated either of 2 different models @ different points in time. @ location or held user, , should never both.

what association or design work this? thanks....

i use polymorphic association this, try this:

item.rb

  belongs_to :check_outer, :polymorphic => true 

user.rb

  has_many :items, :as => :check_outer 

location.rb

  has_many :items, :as => :check_outer 

you need proper database columns support this.

  • on items table, add check_outer_type:string , check_outer_id:int

for more information on polymorphic associations, check out: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -