I have two models. One is an Item model and the other is an Event model.
What I am needing to do is this:
An Item has to be “checked out” to an Event. The Event will show all Item that are apart of said event. Once event is over, user will check the Item back in. You should still be able to see all items that were apart of said event.
This is how my current models are:
Item Model:
class Item < ActiveRecord::Base
has_and_belongs_to_many :events
end
Event Model:
class Event < ActiveRecord::Base
has_many :items
end
I read elsewhere I could have a Model like this that I can use the :though with. This Model would then have all the items that each event has.
EventList
class EventList < ActiveRecord::Base
belongs_to :item
belongs_to :event
end
I would like the URL routes to look like:
domain.com/Event/:id/items/
domain.com/Event/:id/items/(checkedin or checkedout)
Is this the correct way of doing things? Should I have another table and use the :though => name for this particular issue?
You are mixing the two up a little bit. If your using
has_and_belongs_to_manyboth models should define it as that. ie.Event Model:
If you want to use a
:throughtable you define both with justhas_many.As for which one you should use. They are both correct. The link below explains when to use either one. (Basically if you want to do stuff with the join table use the
:throughway, otherwisehas_and_belongs_to_manyis fine)http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many