Has_many and foreign_key question

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

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… :slight_smile:

Cheers,
Giuseppe

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… :slight_smile:

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.

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

Tom Z Meinlschmidt <[email protected]…> 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’)”
}) %>

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