# Has\_many and foreign\_key question

**URL:** <https://www.ruby-forum.com/t/has-many-and-foreign-key-question/102247>\
**Category:** Rails\
**Created:** [August 13, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/has-many-and-foreign-key-question/102247 "2006-08-13T23:05:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@jsma](https://www.ruby-forum.com/u/jsma)\
**Post date:** [August 13, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/has-many-and-foreign-key-question/102247/1 "2006-08-13T23:05:36Z")

</div>

Alright, noob here, etc. etc.  
Trying to figure out the has\_many usage. If I understand correctly, when  
you declare that a table/class has\_many whatevers, the rails assumption  
is that there will be a foreign key in the whatevers table pointing back  
to the ‘id’ primary key in the first table. This doesn’t seem to be the  
only way to have a ‘has\_many’ relationship, and I can’t seem to get  
override the rails assumptions for my tables. I have two tables,  
‘properties’ and ‘jurisdiction\_rates’.

class CreateProperties \< ActiveRecord::Migration  
def self.up  
create\_table :properties do |t|  
t.column :address, :string  
t.column :jurisdiction\_id, :string  
end  
end

class CreateJurisdictionRates  
def self.up  
create\_table :jurisdiction\_rates do |t|  
t.column :jurisdiction\_id, :string  
t.column :tax\_rate, :string  
t.column :type, :string  
end  
end

Rails of course sets up auto incrementing ‘id’ columns on both tables.  
Now there are many properties in the same tax jurisdiction that will  
have the same rates applied to them, so all properties in  
‘jurisidiction\_id’ = 999 have up to three tax ‘type’ (e.g. state, county  
and local) associated with them. Clearly, I’d prefer to have the  
‘jurisdiction\_id’ in ‘properties’ join to  
‘jurisdiction\_rates.jurisdiction\_id’ rather than a ‘properties\_id’,  
since if I did it the latter way, I’d have millions of records (three  
tax rates entries for each property ‘id’, rather than 200 records for  
each jurisdiction ‘id’). Using a habtm also seems like overkill. Am I  
just not thinking about my table design intelligently? Or is there some  
way to get rails to play with this structure? I’ve tried to use:

class Property \< ActiveRecord::Base  
has\_many :jurisdiction\_rates, :foreign\_key =\> “jurisdiction\_id”  
end

class JurisdictionRates \< ActiveRecord::Base  
belongs\_to :properties, :foreign\_key =\> “jurisdiction\_id”  
end  
This doesn’t seem to work at all. I can’t figure out how to tell rails  
to use the ‘jurisdiction\_id’ on both tables to do the join. Any  
thoughts? Maybe I just need some sleep and my mistakes will be obvious.

jsma

---

<div class="post-metadata">

**Author:** ![jsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@jsma](https://www.ruby-forum.com/u/jsma)\
**Post date:** [August 14, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/has-many-and-foreign-key-question/102247/2 "2006-08-14T14:46:37Z")

</div>

Well maybe jurisdiction/tax rates question is obscure, but the situation  
seems to be the same if we think instead of ‘zip/postal’ codes. If the  
zip/postal codes had multiple attributes in a ‘zips/postal codes’ table  
(perhaps census statistics), would it make sense to strip out  
‘zip/postal code’ from ‘properties’, and make a habtm relationship? This  
doesn’t seem reasonable to me. You would have millions of records in the  
join table just to reattach the zips to the properties. It would make  
more sense to leave the zip codes in the properties table and then link  
these zip codes to zero or more rows in a zip-code attributes table  
(which may only actually total a couple of hundred rows). I guess the  
main difference is that although each property can have many attributes  
through the zip (or in my case jurisdiction code), the property only  
ever has one zip/jurisdiction code attached to it, not ‘many’.  
Currently, I can successfully look up the rates in my current setup  
(which is an ungodly Zope ZPT/DTML/SQL frankenstein) doing the table  
join in raw sql. I inherited the zope mess and it’s unfortunately all I  
know right now. But I’m trying to learn a ‘reasonable’ framework so I  
can more easily get stuff done. So maybe my questions are misplaced  
since I may be trying to cram my frankenstein into the ‘rails way’.

Anyway, thanks for your feedback and taking the time to respond. Hope  
this clarifies my own thinking a little and perhaps suggests new ways to  
solve this problem?

Giuseppe B. wrote:

> I am not sure I fully understand your schema, but at first sight it does  
> look like a habtm situation, with a join table in between, as in the  
> following (pseudocode):
> 
> create\_table :properties  
> column :address, :string
> 
> create\_table :properties\_rates  
> column :property\_id, :integer  
> column :rate\_id, :integer
> 
> create\_table :rates  
> column :jurisdiction, :string  
> column :tax\_rate, :string  
> column :type, :string
> 
> If, however, you need to attach more information to the jurisdiction,  
> then jurisdictions should be its own model:
> 
> create\_table :properties  
> column :address, :string  
> column :jurisdiction\_id, :integer
> 
> create\_table :jurisdictions  
> column :label, :string  
> column :some\_other\_info, :text
> 
> create\_table :rates  
> column :jurisdiction\_id, :integer  
> column :tax\_rate, :string  
> column :type, :string
> 
> in the Property class, you would then use:  
> has\_many :rates, :through =\> :jurisdictions
> 
> Again, this may or may not make sense depending on if I understand  
> correctly your problem… 🙂
> 
> Cheers,  
> Giuseppe

---

<div class="post-metadata">

**Author:** ![jsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@jsma](https://www.ruby-forum.com/u/jsma)\
**Post date:** [August 14, 2006, 9:27am UTC](https://www.ruby-forum.com/t/has-many-and-foreign-key-question/102247/3 "2006-08-14T09:27:42Z")

</div>

I am not sure I fully understand your schema, but at first sight it does  
look like a habtm situation, with a join table in between, as in the  
following (pseudocode):

create\_table :properties  
column :address, :string

create\_table :properties\_rates  
column :property\_id, :integer  
column :rate\_id, :integer

create\_table :rates  
column :jurisdiction, :string  
column :tax\_rate, :string  
column :type, :string

If, however, you need to attach more information to the jurisdiction,  
then jurisdictions should be its own model:

create\_table :properties  
column :address, :string  
column :jurisdiction\_id, :integer

create\_table :jurisdictions  
column :label, :string  
column :some\_other\_info, :text

create\_table :rates  
column :jurisdiction\_id, :integer  
column :tax\_rate, :string  
column :type, :string

in the Property class, you would then use:  
has\_many :rates, :through =\> :jurisdictions

Again, this may or may not make sense depending on if I understand  
correctly your problem… 🙂

Cheers,  
Giuseppe

John-scott Atlakson wrote:

> Rails of course sets up auto incrementing ‘id’ columns on both tables.  
> Now there are many properties in the same tax jurisdiction that will  
> have the same rates applied to them, so all properties in  
> ‘jurisidiction\_id’ = 999 have up to three tax ‘type’ (e.g. state, county  
> and local) associated with them.

---

<div class="post-metadata">

**Author:** ![jsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@jsma](https://www.ruby-forum.com/u/jsma)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/has-many-and-foreign-key-question/102247/4 "2006-08-15T14:19:52Z")

</div>

Hi,

I need to use link\_to\_remote, but I want to add some values from form to  
the :url - is that possible? Have a large form, and really don’t want to  
send it all over AJAX - with form\_remote\_tag - just to check one phone  
number…

is there any solution?

thx

–  
/znouza

---

<div class="post-metadata">

**Author:** ![jsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@jsma](https://www.ruby-forum.com/u/jsma)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/has-many-and-foreign-key-question/102247/5 "2006-08-15T14:20:23Z")

</div>

Tom Z Meinlschmidt \<tomas@…\> writes:

> Hi,
> 
> I need to use link\_to\_remote, but I want to add some values from form to  
> the :url - is that possible? Have a large form, and really don’t want to  
> send it all over AJAX - with form\_remote\_tag - just to check one phone  
> number…

You have a couple choices, passing params in the URL:

\<%= link\_to\_remote(image\_tag(“sort\_down\_12x11.gif”, :border =\> 0), {  
:complete =\> visual\_effect(:pulsate, “#{container\_id}”, :duration  
=\> 0.5),  
:url =\> url\_for(:action =\> “answer\_swap”, :params =\> (:answer =\>  
idx, :direction =\> 1))  
}) %\>

or using :with:

\<%= link\_to\_remote(image\_tag(“add\_30x30.jpg”, :border =\> 0), {  
:complete =\> visual\_effect(:pulsate, “#{container\_id}”, :duration  
=\> 0.5),  
:url =\> url\_for(:action =\> “answer\_add”, :params =\> pr\_values),  
:with =\> “‘answer=’ + $F(‘an\_answer’)”  
}) %\>
