Ruby on Rails Loan Amortization Schedule - ruby-on-rails

What would be the best way to incorporate a loan amortization schedule in my rails app? Is it possible? Is ruby on rails even a good way to implement something like that? I have built them in excel a few times before, but just can't picture how to do it in rails.

This all depends on the actions your users will need to perform on/with the schedule.
If your goal is simply displaying the schedule, there's no need to persist the entire array of dates/payments/etc. Simply store a start/end date, payment interval, loan amount, interest rate and the payment amount. It isn't heavy work to construct the actual schedule given this information.
If your users should be able to annotate specific items within the schedule, then consider which properties of each item would make good unique identifiers (hint: loan + item date). From there, your models should flesh out easily:
class ScheduleItem
{
date dueDate;
float paymentAmount;
string annotations;
bool isPaid;
...
}
class Schedule
{
int loanId;
date startDate;
date endDate;
...
}
If you do decide you need models (to persist or just to generate views) representing the schedule start with the simplest implementation possible and expand from there. You're most likely having trouble getting started because you're trying to incorporate every variable from the start. Keep it simple. Get things rolling.

Related

Rails 4 - Ordering by something not stored in the database

I am using Rails 4. I have a Room model with hour_price day_price and week_price attributes.
On the index, users are able to enter different times and dates they would like to stay in a room. Based on these values, I have a helper method that then calculates the total price it would cost them using the price attributes mentioned above.
My question is what is the best way to sort through the rooms and order them least to greatest (in terms of price). Having a hard time figuring out the best way to do this, especially when considering the price value is calculated by a helper and isn't stored in the database.
You could load all of them and do an array sort as is suggested here, and here. Though that would not scale well, but if you've already filtered by the rooms that are available this might be sufficient.
You might be able to push it back to the database by building a custom sql order by.
Rooms.order("(#{days} * day_price) asc")

Model design for a calendar

I'm building an application that is based on a calendar and (with basic functionality expected from a calendar).
As the calendar will have such a fundamental part of the application I don't want to rely on any gem but build the calendar myself.
I don't know which route to go: to have a day-model representing each day with a unique record (that events can reference by day_id) OR render the calendar "on the fly" based on a Date class (and events would then reference the date).
In short: What model design would be the most efficient to render a calendar?
You don't need to have a model at all. The following code below will do the trick.
Grab the code from https://gist.github.com/RichIsOnRails/5535564 and drop it in a helper, use like so (haml below, so adapt it to meet your own needs.) Note that i'm rendering events in this calendar, but you don't have to.
= calendar #date do |date|
.text-left=date.day
- if #events_by_date[date]
- #events_by_date[date].each do |event|
.event=event.title
I will do an article on my website in the near future that goes over this in detail. Good luck!
Unless there is something specific about the days you would like to model, you may be able to just have an events model, with each event having a date field. This approach is simpler and I think preferable. Since there are a practically infinite number of dates, one big advantage of this approach is that you won't need to manage all the logical inconsistencies of keeping a finite subset of dates in your database.
Even if you find a need for a day model, you can do this without tying the model to the database, and thus avoid having a record in your database for every possible date. This might be useful if you want a method that tells you if a day is a work day, or if you want to find a collection of days. For more on how to make a model that doesn't require a database table behind it, see this stackoverflow question: Tableless model in rails 3.1

How to model the associations for a task application?

I'm working on a goal completion application, where there can be Goals, Milestones, and Tasks. Milestones belong to goals, goals can have many milestones, and tasks can belong to either goals, milestones, or stand on their own. The information I want to store in each model is as follows:
Goal content:string, est_completion_date:date completed:boolean, completion_date:date
Milestone content:string, est_completion_date:date completed:boolean, completion_date:date
Task content:string, occur_on:date completed:boolean, completion_date:date, days:?
The 'occur_on' field for the Task model is to schedule the task for a custom date. The 'days' field stores what days of the week the task should happen, if it's recurring.
I have two questions.
1) I read about single table inheritance and wondered if this would work for me. Every model is pretty much the same except the Task model has 'days' and 'occur_on', but doesn't have 'est_completion_date'. What's the best way to model all of these associations?
2) I'm not quite sure how to best store the information regarding what days of the week a task should occur on. Should I just make it an associative array with boolean values for each day, or should I have separate fields for each day in the table?
1) Your single table inheritance question is well answered here.
2) I recommend keeping it simple for your days column. Just store a string of digits, with each digit representing a day. So 245 might represent Monday, Wednesday & Thursday. To find tasks that occur on Wednesday you can query with a regular expression, e.g. select * from tasks where days regexp("4");

Creating a Calendar/Planner Application - Ruby on Rails

I am considering developing an application using Ruby on Rails that is a planner of sorts. I would like to give a user the ability to see a list of days, click a particular day, and then add things like: Meals, Expenses, Events, Todos, and Exercises. Really I am doing this for me and my growing family.
I am curious with how best to implement this. I can certainly see that Meals, Expenses, etc. need to belong_to :user but I am curious how to implement the belongs_to :day or something like that. Using the created_at or updated_at wouldn't necessarily allow me to provide views for future dates.
I can see how if I created a Days table and then added days through a time and date field that this would work but seems a little strange to ask people to create the actual days.
Or perhaps instead of that I could just create links to variables that search for #today, #tomorrow, but that would get messy.
I have browsed for gems/plugins but can't find one that works. Ideally a person would be able.
Anyone have any thoughts on how to implement something like this?
There are a number of existing Rails calendars, such as http://github.com/elevation/event_calendar or http://github.com/topfunky/calendar%5Fhelper.
However, to answer your specific question about dates: I don't think there's any need to have Day as a model; simply give each event a start date and time, and an end date and time. Remember that Ruby makes it easy to search based on ranges, so "give me all of the events next week" is a cinch if each event has dates and times associated with it.
I'll give it a shot...
Two tables; users and events. A user has many events and an event belongs to a user. Meal, Expenses, etc. are various types of Event. Within events, you can have fields for start and end time of the events. If needed (lets say an events last over multiple days), you could remove the day/time when events occurs into it's own table.
This way, when displaying the calendar for a user, you can find all the events for that date. If none are found, then display nothing.
What do you think?
I would add a model called "Events" and have a properties of the model to represent start date/time, end date/time. I do not think you need a Days model, you can generate your calendar view from the Date class built into ruby.
I have done same kind of project for the Event management in training institute. At there I used event_calender plug in with rails. (enter link description here)
In there we just need to create Event model only. Then we can easily work with that.

How to handle ids and polymorphic associations in views if compound keys are not supported?

I have a Movie plan table:
movie_plans (id, description)
Each plan has items, which describe a sequence of movies and the duration in minutes:
movie_plan_items (id, movie_plan_id, movie_id, start_minutes, end_minutes)
A specific instance of that plan happens in:
movie_schedules (id, movie_plan_id, start_at)
However the schedule items can be calculated from the movie_plan_items and the schedule start time by adding the minutes
create view movie_schedule_items as
select CONCAT(p.id, '-', s.id) as id,
s.id as movie_schedule_id,
p.id as movie_plan_item_id,
p.movie_id, p.movie_plan_id,
(s.start_at + INTERVAL p.start_minutes MINUTE) as start_at,
(s.start_at + INTERVAL p.end_minutes MINUTE) as end_at
from movie_plan_items p, movie_schedules s
where s.movie_plan_id=p.movie_plan_id;
I have a model over this view (readonly), it works ok, except that the id is right now a string.
I now want to add a polymorphic property (like comments) to various of the previous tables. Therefore for movie_schedule_items I need a unique and persistent numeric id.
I have the following dilemma:
I could avoid the id and have movie_schedule_items just use the movie_plan_id and movie_schedule_id as a compound key, as it should. But Rails sucks in this regard.
I could create an id using String#hash or a md5, thus making it slower or collision prone (and IIRC String#hash is no longer persistent across processes in Ruby 1.9)
Any ideas on how to handle this situation?
I think you've built your models a little too complex.
MoviePlan is a pattern that you use for every day's schedule of movies? I'd very much recommend to drop the MovieSchedule and instead allow the user to copy an existing MoviePlan. This will a) make your design simplier and b) allow more freedom for the user as schedules often differ in reality. An extra matinee on Saturdays, a special foreign movie on wednesdays, that's all what I see at my favourite cinema.
Lastly, making comments on a movie of a specific movie plan and schedule doesn't make any sense to me, or is this some customer management thing like "my seat in the 7pm show of Funny Movie was dirty"?

Resources