Forum: Ruby on Rails Help with Non-model classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
540acafebb6daddf6cc219facaf94ccd?d=identicon&s=25 Adam van den Hoven (Guest)
on 2006-01-11 18:35
(Received via mailing list)
Hey guys,

I've been trying to teach myself Rails by building a relatively
complex app for work. I've started it twice and now I'm thinking
about restarting it a third time.  I need some help with some of my
code.

I'm creating an app to help us manage time. We have a number of
developers on a number of teams who are assigned to any number of
projects or change orders (small changes for specific clients). It
can be a real challenge to communicate to everyone who is available
to do this new project or change when.

This app tracks projects, the people who they are assigned to and how
much effort they have put in, or plan to put in to those assignments
each week.

Because this is effectively a weekly calendar, I created the idea of
a week as a range of dates. In my original version, I had a weeks
table in my database and the start and end dates of all my time
sensitive objects were week_id. In the second rendition, I still had
my weeks table but everything date sensitive used a date, rather than
an ID. This means that I don't have to have a Week model, only a
simple class.

My problem is that I'm not 100% sure where to put the class, how to
include it in my app, and how to set up the testing (I have a lot of
tests from the second round that would still be valuable).

Any thoughts?
Adam
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-11 19:04
Why not create a class like Task?

table tasks
-----------
id - integer
date - date
task - text

Then you can do a BETWEEN sql-query to find every task in a certain
week.

Task.find(:all, :conditions => ['date BETWEEN ? AND ?', Time.now,
1.week.from_now])

I'm not sure if this works though.
D8cb8c8cd40ddf0cd05241443a591868?d=identicon&s=25 Duane Johnson (Guest)
on 2006-01-11 19:09
(Received via mailing list)
On Jan 11, 2006, at 10:29 AM, Adam van den Hoven wrote:

> can be a real challenge to communicate to everyone who is available
> still had my weeks table but everything date sensitive used a date,
> rather than an ID. This means that I don't have to have a Week
> model, only a simple class.
>
> My problem is that I'm not 100% sure where to put the class, how to
> include it in my app, and how to set up the testing (I have a lot
> of tests from the second round that would still be valuable).
>
> Any thoughts?
> Adam

Models don't have to have a database connected to them, nor do they
have to be ActiveRecord::Base-derived.  Is there any reason you
shouldn't put it in the models directory?  Doing so will still give
you the benefit of auto-required classes as long as you follow the
naming convention.

e.g.
# week.rb:
class Week
   # ... your code here ...
end

Duane Johnson
(canadaduane)
http://blog.inquirylabs.com/
77fc3e4b91c1ab0440604f5dd795c442?d=identicon&s=25 Adam Van Den Hoven (Guest)
on 2006-01-11 21:23
(Received via mailing list)
I didn't realize that simply putting a file in /app/model makes it a
model. I'll do it that way. Will fixtures still work without a table?

Adam
D8cb8c8cd40ddf0cd05241443a591868?d=identicon&s=25 Duane Johnson (Guest)
on 2006-01-11 23:40
(Received via mailing list)
On Jan 11, 2006, at 1:21 PM, Adam Van Den Hoven wrote:

> I didn't realize that simply putting a file in /app/model makes it
> a model. I'll do it that way. Will fixtures still work without a
> table?
>

Well, sort of.  You're going to have to do a little more on your own,
but it's not that bad since most of what Rails helps with is reducing
the work related to the database.

To get a fixture, just load it via yaml:

require 'yaml'

my_fixtures = YAML.load(IO.read(filename))

Where 'filename' is the name of the fixture file you want to load.

Duane Johnson
(canadaduane)
http://blog.inquirylabs.com/
This topic is locked and can not be replied to.