Re: [Rails] Using ActiveRecord to add data on record load
On 15/04/2013, at 1:27 AM, Peter Hicks <peter.hicks@poggs.co.uk> wrote:
> All,
>
> I've been struggling to find a neat solution to this problem for a couple of weeks now, so I'd love to hear anyone else's opinion. I'm on Rails 3.2.13 and Ruby 1.9.3-p392.
>
> I have a table of train schedules, and a table of train schedule locations with a has_many association. Each schedule has a set of validity dates, e.g. each Friday between 1st January and 28th September. The times in the schedule locations table are all 'seconds since midnight on the day the train runs': where a train runs over midnight, the number of seconds is greater than 86400.
>
> Whilst this is reasonably 'clean', I want to re-engineer the schedule location model to return a Time object for each location, calculated on the fly based on the date I queried the schedule model for, i.e. Schedule.where(:runs_on => Date.parse('2013-01-01')) will return a Schedule model with an instance variable 'runs_on', and when I request the associated schedule locations, the 'arrival_time' and 'departure_time' fields are auto-calculated based on the instance variable in the schedule model.
>
> Is this a sensible way to do things? Is this even possible or am I over-engineering it?
>
>
> Peter
>
Let's do some architecture and modelling, shall we?
From what I can tell, the classes in your system are TrainSchedule and ScheduleLocation. Right off the bat these aren't clear to me. What are the responsibilities of each class? I can get a pretty good guess at TrainSchedule easily, but not ScheduleLocation. Is schedule location a train schedule taylored to a particular location?
A model should exist to provide allow a set of messages. What do your models need to do and know? It's very useful to think of things in terms of a set of things that you can send to your model (its messages) because this gives you an interface to deal with as a user of this model.
So it's good to describe what each class is for... and to deeply ask yourself what each class *is*. From your initial modelling, I'm not 100% sure what you want to put in and get out of the system.
Do you want to put in a series of locations (which know the distance between themselves along the trainline), and from there, you can build a schedule dynamically? That sounds like a great way to do it, however it's very different than what you've proposed.
Or do you simply want a schedule to hold a list of locations and the times that the train will arrive at a certain stop at? This seems more in line with what you've described already exists.
How you architect the internal workings of your classes & system should be dictated to by the external requirements on that system and if you're not telling us what those requirements are, we cannot help you architect the organisation of the internal workings of your classes to best obtain those outcomes or requirements.
Julian
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home