I`m working on a project that controls the equipment lendings to users.
It displays the lending view that renders a form where user can choose the equipment that he/she wants through a select box.If the equipment is not available in select box, user have to register the equipment then it will appear in that select box in lending view's form.
Model Association:
Lending has_many Equipment
Equipment belongs_to Lending
The doubt is: If Equipment belongs_to Lending then I must put the lending_id in Equipment.
But if user access the new equipment view and try to register a new equipment it will miss the lending_id.
How can I solve this?
My guess is what you really want is for lending to be a many-to-many mapping of users to equipments.
class User < ActiveRecord::Base
has_many :lendings
has_many :equipments, :through => :lendings
# etc
end
class Equipment < ActiveRecord::Base
has_many :lendings
has_many :users, :through => :lendings
# etc
end
class Lending < ActiveRecord::Base
belongs_to :user
belongs_to :equipment
# etc
end
You can write methods to give the "current" user for an equipment via the latest active lending, and vice-versa, plus you have a convenient history of all lendings of an item of equipment.