Quoting Andrew Otwell <andrew-uQjPo4GTFqgS+FvcfC7Uqw@public.gmane.org>:
> Eep, you''re right: I was too hasty. However, your plurals like
has_many
> and has_and_belongs_to_many definately should point to plural
> associations. So
>
> belongs_to :location #i.e belongs to just one location
> has_many :addresses # i.e. has multiple addresses, not one address
>
> try that.
>
Ok, so I now have this, but I still have the same problem with state_id 49 and
53 being selected (and not 51 which is the one stored in the table). Also are
all these pluralization rules documented somewhere?
Thanks
Amith
============================================================================app/views/address/edit.rhtml
============================================================================
....
<%= collection_select(''address'',
''state_id'', State.find_all, ''id'',
''state_cd'')
%>
....
============================================================================address.rb
============================================================================class
Address < ActiveRecord::Base
has_and_belongs_to_many :people, :join_table =>
"person_addresses"
belongs_to :location
belongs_to :state
end
============================================================================state.rb
============================================================================class
State < ActiveRecord::Base
has_many :addresses
end
============================================================================mysql>
desc addresses;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | | PRI | NULL | auto_increment |
| street_address | varchar(100) | | | | |
| apt_no | varchar(20) | YES | | NULL | |
| city | varchar(50) | YES | | NULL | |
| state_id | char(2) | YES | | NULL | |
| zip_cd | varchar(10) | YES | | NULL | |
| country | varchar(50) | YES | | NULL | |
| location_id | int(10) unsigned | YES | MUL | NULL | |
| last_updated | timestamp(14) | YES | | NULL | |
| street_address2 | varchar(100) | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
mysql> desc states;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | | PRI | NULL | auto_increment |
| state_cd | char(2) | | | | |
+----------+------------------+------+-----+---------+----------------+