I am currently working on an application which manages rooms, their availability, pricing, etc. on different days.
I have already made the application with general management, like having on price for the whole year and the room will be available for the whole year.
What I want to achieve is that the user should be able to set price for each day, if he doesn’t sets the price for a particular day, then the general price will be for that day, he should also be able to set whether the room is available on a particular day or not.
For adding this feature I am using FullCalendar.
Here is how I have done it
There is a model called as Calendar which belongs to rooms and rooms has many calendar.
The calendar table consist of the following columns:
Price : float
availability : boolean
Date : Date
Room_id : integer
FullCalendar requires me to send the data as Json, so i query for all the dates within the current month and show the title as price and description as available or not.
But implementing it this way requires me to add details to calendar for each day even if the user is setting a general price for each and every day for the room. That is, 365 records for each room in the calendar table. Which I have to set at the time of creation of the room model.
Can anyone suggest me a better way to achieve this feature in much better way.
Modeling questions are tough to answer on the forum, since it’s hard to include all the information necessary to make a good recommendation.
Let me suggest a general approach for dealing with modeling questions:
Ask yourself what is giving you pain right now. For your situation, it’s that you need to create hundreds of Calendar records when you create a Room.
Try to think of a fix for the problem you’re having. What if you added a method on Room called availability_this_month that returns a collection of Calendars when they exist for a certain day, but otherwise returns JSON with some defaults in it?
Once you have an idea that might fix the problem, make a branch and test it out.
Is what you’ve done better? Merge that branch back. If not, delete the branch and try something else out.
Getting good at modeling is almost entirely building up experience through trial and error. Don’t be shy about trying different ideas. Git makes it extremely easy to test something with no long-term consequences.
Lots of newer Rails devs ask for modeling help, but I think there’s a lot of value in just trying things out for yourself and learning the pros and cons of different approaches.
One thing that jumps out at me, is that a Calendar is a way to present data, but not actually the data itself. The data could consist of Bookings, or Special Prices, Cancellations etc, which would all have associated dates or date ranges.
I think the Calendar class should maybe not be backed by ActiveRecord, but act more as a presenter to create the appropriate JSON needed for the plugin you are using.
Hey @benorenstein , Can you suggest me a way so that i can add data in an active record object after it as been fetched from the database, without saving that data in database, for sending the data to the view.