Beginner questions on attributes and arrays


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:

  1. How do I declare a variable (attribute?) in a model that doesn’t
    directly correlate to a database column?
  2. 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] = " "

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.

I’ve written something similar to this a while back, but not using

Mapping it across to your application, the approach I used was to have
a single text field named “territory_list”, and users would enter
something like “1,2,6-13,23-34,50,97” into it, indicating that the
sales rep was responsible for this set of territories.

First, the users liked it - it was analogous to how they select ranges
of pages to print in MS Word, so they were familiar with the concept.

Second, it fit into a small section of screen area, and didn’t
dominate the page. As there was a bunch of other content to fit on
that same page, this was a good thing.

It’s not difficult to parse out such a field into its underlying
values (i.e. 1,2,6,7,8,9,10,11,12,13,23,24,…) and then update your
database appropriately. It’s a bit more difficult to read existing
data out of the database and re-create such a list for update
purposes, but it’s not complex as much as annoying…

I would definitely include the has_many/belongs_to relationship in
your model. Although you might not be using it for entering and
maintaining the linkage, I can see it’d be very useful to have an
AJAX-style div onscreen that shows the names of the territories (in
semi real time) as a user is entering in their corresponding numbers.
There may also be other interface requirements that appear later -
maybe some/all of your users find they’d rather type in the first few
letters of the territory name and have it appear for them, because
they can’t remember the numeric codes of several hundred territories.
These both strike me as particularly useful uses of the AJAX stuff
that Rails brings to the party.

Good luck

Dave M.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs