# Oscommerce modeling question

**URL:** <https://www.ruby-forum.com/t/oscommerce-modeling-question/134675>\
**Category:** Rails\
**Created:** [April 13, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/oscommerce-modeling-question/134675 "2008-04-13T21:25:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tubahead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@Tubahead](https://www.ruby-forum.com/u/Tubahead)\
**Post date:** [April 13, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/oscommerce-modeling-question/134675/1 "2008-04-13T21:25:03Z")

</div>

All,

Many thanks for the previous help I have received from this list … I  
am hoping someone can point me in the right direction here. I am  
attempting to bolt on a ROR admin system for an existing oscommerce  
store, and so far have been fairly successful.

My setup is a basic oscommerce install, which is apache/php/mysql. I  
have abbreviated the tables to include only information relevant to  
my issue.

Database Tables in question  
customers:  
customers\_id int(11) NOT NULL auto\_increment,  
customers\_firstname varchar(32) NOT NULL,  
customers\_default\_address\_id int(11) default NULL,  
PRIMARY KEY (customers\_id)

Example row in customers:  
customers\_id = 50  
customers\_firstname = bob  
customers\_default\_address\_id = 1

address\_book:  
address\_book\_id int(11) NOT NULL auto\_increment,  
customers\_id int(11) NOT NULL,  
entry\_firstname varchar(32) NOT NULL,  
entry\_postcode varchar(10) NOT NULL,  
entry\_state varchar(32) default NULL,  
entry\_country\_id int(11) NOT NULL default ‘0’,  
entry\_zone\_id int(11) NOT NULL default ‘0’,  
PRIMARY KEY (address\_book\_id),

Example rows in the address\_book table:  
address\_book\_id = 1  
customers\_id = 50  
entry\_firstname = bob  
entry\_postcode = 45619  
entry\_state = Alaska  
entry\_country\_id = 223  
entry\_zone\_id = 2

address\_book\_id = 11  
customers\_id = 50  
entry\_firstname = bob  
entry\_postcode = 40000  
entry\_state = Alaska  
entry\_country\_id = 223  
entry\_zone\_id = 2

zones:  
zone\_id int(11) NOT NULL auto\_increment,  
zone\_country\_id int(11) NOT NULL,  
zone\_code varchar(32) NOT NULL,  
zone\_name varchar(32) NOT NULL,  
PRIMARY KEY (zone\_id),  
KEY idx\_zones\_country\_id (zone\_country\_id)

Example row in the zones table:  
zone\_id = 2  
zone\_country\_id = 223 (this is the US)  
zone\_code = AK  
zone\_name = Alaska

The customers table has the name/email/basic information for a  
customer. The address\_book table (note the non plural table name)  
contains multiple mailing/shipping addresses for a customer. The  
customers\_default\_address\_id in the customers table denotes which is  
the default address to ship to. In my example, bob has two addresses  
in Alaska.

My issue is, how can I represent this information in a collection of  
models such that I can use the address information properly.

My existing customer model works just fine.  
I attempted to add an “address\_book” model, but ran into some trouble  
with the non-plural table name plus the fact that it had an  
underscore.  
The fact that the state two letter abbreviation is actually stored in  
the zones table, left me a little stuck.

My goal was to be able to do something like this:

c = Customer.find 50 - this would find the customer record for bob -  
this works great.  
c.customers\_state would return the current state of the current  
default address (the two letter, requiring a hit of the address\_book  
and zones table).  
c.addressbooks would return a list of all the addresses for a  
customer.

My existing solution to this problem was to create a view in mysql for  
my customer information and have it only use the current default  
address. This was going to be an OK solution, but I discovered the  
hosting service for my site is using a downlevel version of mysql, and  
views are not possible.

I would be glad to share my completed mapping of rails to oscommerce  
when I am done if anyone is interested …

Note: I am still a newbie on rails - please tell me if this is the not  
the right place for this question.  
Thanks for any help anyone can provide.

Dustin

---

<div class="post-metadata">

**Author:** ![Tubahead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@Tubahead](https://www.ruby-forum.com/u/Tubahead)\
**Post date:** [April 15, 2008, 4:28am UTC](https://www.ruby-forum.com/t/oscommerce-modeling-question/134675/2 "2008-04-15T04:28:38Z")

</div>

This might not be exactly what you wanted, but after thinking about  
your situation for a while, I decided that it would be best to point  
you to some applicable documentation - primarily because I don’t know  
if I could come up with a good solution for you! For your models, you  
can try to use table\_name ([Peak Obsession](http://api.rubyonrails.com/classes/)  
ActiveRecord/Base.html#M001396) if you have singular/plural problems.  
You can also customize the inflector in config/environment.rb, I  
believe. For associations, you’ll need to define your classes/foreign  
keys since they don’t happily marry to the rails standards. Look at  
the :class\_name and :foreign\_key options to different associations  
([Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Associations/)  
ClassMethods.html).

Sorry. I feel like I’m taking the easy way out, but others might be  
able to provide you with some specifics.

-Kyle

---

<div class="post-metadata">

**Author:** ![Tubahead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@Tubahead](https://www.ruby-forum.com/u/Tubahead)\
**Post date:** [April 15, 2008, 6:12am UTC](https://www.ruby-forum.com/t/oscommerce-modeling-question/134675/3 "2008-04-15T06:12:10Z")

</div>

No problem … any help at all is appreciated.  
I have been pondering it a bit as well, and this is a far as I have  
gotten.

I can create a model to match the address book like this:  
./script/generate model addressbook --skip-migration  
Then modify the model and force it to use my strange table name using  
the table\_name option you mentioned. Changing the inflector is  
something I need to read a little more about, but that sounds pretty  
close.

That gets me a list of address book entries, but I am not sure how to  
query my model for the default one.  
My guess is something like:  
c = Customer.find 50  
@default\_address\_book\_id = c.customers\_default\_address\_id  
@default\_address = c.addressbooks.find(:all, :conditions {:id =  
@default\_address\_book\_id} )

This still leaves me with the “zones” table, and I am still unsure how  
I should handle this. Let me just take another shot at this whole  
problem, maybe I haven’t applied enough brute force yet.

Thanks for your help !!

Dustin

---

<div class="post-metadata">

**Author:** ![Tubahead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@Tubahead](https://www.ruby-forum.com/u/Tubahead)\
**Post date:** [April 15, 2008, 9:02am UTC](https://www.ruby-forum.com/t/oscommerce-modeling-question/134675/4 "2008-04-15T09:02:00Z")

</div>

Maybe create a view that ‘looks right to rails’ and maps back to your  
real db?

---

<div class="post-metadata">

**Author:** ![Tubahead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@Tubahead](https://www.ruby-forum.com/u/Tubahead)\
**Post date:** [April 16, 2008, 2:55am UTC](https://www.ruby-forum.com/t/oscommerce-modeling-question/134675/5 "2008-04-16T02:55:27Z")

</div>

OK … I have made some progress, it was somewhat easier than I  
thought.

Customer Model:  
class Customer \< ActiveRecord::Base  
set\_table\_name ‘rscustomers’  
set\_primary\_key :customers\_id  
has\_many :addressbooks, :foreign\_key =\> “customers\_id”  
end

Addressbook Model:  
class Addressbook \< ActiveRecord::Base  
set\_table\_name ‘address\_book’  
set\_primary\_key ‘address\_book\_id’  
belongs\_to :customer  
## The following line is WRONG, not sure just how to fix it yet.  
has\_one :zone, :foreign\_key =\> “zone\_id”  
end

Zone Model:  
class Zone \< ActiveRecord::Base

end

So, here is what works so far.  
c = Customer.find 50

## Works great, get back the correct customer.

a = c.addressbooks.find(:first) – returns the first address book  
entry for a customer

## Works great, returns the correct address books for the customer

a.zone  
=\> nil

## This is my current failure point, and I see the issue but haven’t

found how to fix it yet.

## There is a record that matches

From my development.log:  
Addressbook Columns (0.002975) SHOW FIELDS FROM `address_book`  
Zone Load (0.000500) SELECT \* FROM `zones` WHERE (zones.zone\_id =  
50) LIMIT 1

When attempting to find the zone information for my address book  
entry, the model is querying from the zones table where the zone\_id =  
customers id.

Note again the table structure:

customers:  
address\_book\_id = 1  
customers\_id = 50  
entry\_firstname = bob  
entry\_postcode = 45619  
entry\_state = Alaska  
entry\_country\_id = 223  
entry\_zone\_id = 2

zones:  
zone\_id int(11) NOT NULL auto\_increment,  
zone\_country\_id int(11) NOT NULL,  
zone\_code varchar(32) NOT NULL,  
zone\_name varchar(32) NOT NULL,  
PRIMARY KEY (zone\_id),  
KEY idx\_zones\_country\_id (zone\_country\_id)

The query should be trying to find the zone in the zone table where  
the entry\_zone\_id = zone\_id.

I am reading through the active record docs to see if I can figure it  
out …

Dustin

---

<div class="post-metadata">

**Author:** ![Tubahead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@Tubahead](https://www.ruby-forum.com/u/Tubahead)\
**Post date:** [April 16, 2008, 3:13am UTC](https://www.ruby-forum.com/t/oscommerce-modeling-question/134675/6 "2008-04-16T03:13:01Z")

</div>

Got it … here are my completed model definitions:

class Addressbook \< ActiveRecord::Base  
set\_table\_name ‘address\_book’  
set\_primary\_key ‘address\_book\_id’  
belongs\_to :customer  
belongs\_to :zone, :foreign\_key =\> “entry\_zone\_id”  
end

class Customer \< ActiveRecord::Base  
set\_table\_name ‘rscustomers’  
set\_primary\_key :customers\_id  
has\_many :addressbooks, :foreign\_key =\> “customers\_id”  
end

class Zone \< ActiveRecord::Base  
set\_primary\_key “zone\_id”  
has\_many :addressbooks, :foreign\_key =\> “entry\_zone\_id”  
end

Thanks for the help, I will try to repay in kind as I get a little  
more experience.

Dustin

---

<div class="post-metadata">

**Author:** ![Tubahead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@Tubahead](https://www.ruby-forum.com/u/Tubahead)\
**Post date:** [April 15, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/oscommerce-modeling-question/134675/7 "2008-04-15T15:42:37Z")

</div>

That was my first thought, but I am stuck with mysql 4.x, which  
doesn’t have views as a feature. That comes in mysql5. I had  
considered doing a very wasteful cron script to build me a corrected  
table to query on, but it seemed better to stick with an actual fix or  
a view.

Dustin

On Apr 15, 3:02 am, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
