Rob B. wrote:
rec = MyTable.find_or_create_by_year(2007)
But I second Michael’s question. If you explain what your table
really needs to do (i.e., why is multiple records for the same
“year” desirable), then perhaps you can get a better response.
My gut feeling is that you are looking for a situation where Year
has_many Numbers (i.e., not one table, but two), but you haven’t
presented enough information to really reach that conclusion.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
I’m getting a little annoyed of --MY-- inability to explain my needs…
Ok, i’ll try again (sorry, my first language is spanish).
Ok, here we go:
Imagine you need to record things that happen every year. Events. Ok, so
you begin like this:
Table:
Events_in_2007
Records:
id, description
So every new record you get a nice and shiny, unique record for every
new event that happens:
Events_in_2007:
id, description
1, “First event of 2007”
2, “Second event of 2007”
3, “Fourth event of 2007” << Oh oh, the user thinks this is the Fourth
event, but dosn’t matter, by the id of the table i know it’s not the
fourth but the third record, so let’s go on.
4, “Very important event”
5, …
Ok, you have the picture.
NOW: one year goes by, and i need to keep records for the new year. What
do i do? Well, first thing i come up with is opening an Events_in_2008
table!
Table:
Events_in_2008
Records:
id, description
Events_in_2008:
id, description
1, “First event of 2008”
2, “Second event of 2008”
3, “Fourth event of 2008” << Oh oh, the user thinks this is the Fourth
event, but no matter, by the id of the table i know it’s not the fourth
but the thirs, so let’s go on.
4, “Very important event”
5, …
And that’s all, but…
Obviusly, to create aditional tables for every year is not an option. I
need to merge an arbitrary number of “Events_of_INSERT-A-YEAR” tables
into a single structure, making imposible to have two events with the
same number of event for a given year.
WHY? Because my client orgranizes the information like that. I absolutly
cannot change that, he has a lot of boxes labeled with the year, in
every box he has a paper representing an event, every paper has a number
that is not repeating itself for that year:
BOX FOR 2006
| |
| etc… |
| 3) document for Event three of 2006 |
| 2) document for Event two of 2006 |
1) document for Event one 0f 2006 |
BOX FOR 2007
| |
| etc… |
| 4) document for Event four of 2007 |
| 3) document for Event three of 2007 |
| 2) document for Event two of 2007 |
1) document for Event one 0f 2007 |
BOX FOR 2008
ETC…
My application has to enforce that behaviour so no two documents for a
given year could have the same document number.
I want to model this with ActiveRecord, and don’t know how