Hello,
I am trying to do something and I’ve been reading my Agile Web
Development with Rails book and doing internet searches for several
hours now and I have made no progress.
Here’s the issue: I have a table of sales reps that is linked to a
table of territories. There are around a thousand territories that are
numbered and each sales reps has many territories. I will be storing
the sales rep id in the territory table to associate the two tables.
I have not been specifying a “belongs to” and “has_many” relationship in
my models because I need to be able to take a range of territories (for
example 34-79) and since there are so many territories, I need to do
this with text fields and not an enormous select list. I believe it
will be easier for me to do this by writing custom methods and then
doing custom SQL that will associate the territories after_create.
To accept the data, my form has multiple groups of two text fields each.
This should allow the user to enter a range of territories (43-49) as
well as a single territory by leaving the second field blank. I will
have 9 groups of these text field combinations to allow the user to
enter many territories or ranges of territories.
I know what I want to do in my mind, but I can’t get past step one. It
has brought up some simple questions that I hope someone can answer:
- How do I declare a variable (attribute?) in a model that doesn’t
directly correlate to a database column? - How do I declare a two dimensional array in a model so that I can
receive data from a form in an array?
If it helps clarify what I am trying to do, here is what I think my HTML
should look like:
<%= text_field(“sales_rep”, “territories[1][1]”, :size => 6) %> -
<%= text_field(“sales_rep”, “territories[1][2]”, :size => 6) %>
<%= text_field(“sales_rep”, “territories[2][1]”, :size => 6) %> -
<%= text_field(“sales_rep”, “territories[2][2]”, :size => 6) %>
<%= text_field(“sales_rep”, “territories[3][1]”, :size => 6) %> -
<%= text_field(“sales_rep”, “territories[3][2]”, :size => 6) %>
This of course gives me a “undefined method `territories[1][1]’ for
#SalesRep:0xb76bc074” error because there is no territories attribute
in my model.
I try to declare it in my model like this (and around 12 variations of
this which are all apparently incorrect):
attr_accessible :self.territories[3][2]
def initialize
self.territories[1][1] = " "
self.territories[1][2] = " "
self.territories[2][1] = " "
self.territories[2][1] = " "
self.territories[3][1] = " "
self.territories[3][1] = " "
end
but I’m not sure how I am supposed to declare a two dimensional array in
my model so I can receive data from a form.