Bart, your question sounds like its more about schema design than
Rails. I recommend you begin by working through a guided example like
the todo list, and then have a good look at
http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html.
You might try this for your reservation example:
class Resource < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :resources
end
Rails will then use an implicit mapping table between resources and
users. Rails will expect that table to be called "resources_users",
because it keeps track of associations between users and resources. So
in effect that mapping table is where information on reservations is
stored.
If you want to disallow reservations with overlapping timeframes on a
single resource, your mapping table will become more complex, as you
need to include fields such as "reservation_begin_date" and
"reservation_end_date". When that becomes the case I usually use a
"manual" mapping table. So, I would not use
"has_and_belongs_to_many"
to connect "resources" with "users", but instead I would
link both of
those with an explicit "reservations" tables, using
"has_many"
associations.
I recommend cup of coffee + http://ar.rubyonrails.org :)
cheers,
Gerret
On 10/19/05, Bart Braem
<bart.braem-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hello,
>
> As a toy project and a first contact with Rails I am trying to figure out a
> model for a reservation project. The scheme is quite simple: there are a
> limited number of resources which can be reserved by multiple users for a
> fixed time. What is a good model for doing this in Rails?
> I came up with resource has_one reservation and then many reservations have
> many users. But how should I turn that into a nice Rails model? Let alone a
> good controller?
>
> Help welcome.
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>