This should be easy, but I''m doing something wrong. When edit.rhtml
builds
the drop down boxes for my "Edit Address Page" it shows all the states
correctly but does not select the current value (which should be 51 - VA). In
fact it selects value''s 49 *and* 53 when the only row in the address
table has
a state_id of 51. Anyone know what I''m doing wrong? Here is what I
have
=============================================================================Address
Edit View
=============================================================================
<%= collection_select(''address'',
''state_id'', State.find_all, ''id'',
''state_cd'')
%>
=============================================================================Address
& State models
=============================================================================
class Address < ActiveRecord::Base
has_and_belongs_to_many :person, :join_table =>
"person_addresses"
belongs_to :location
belongs_to :state
end
class State < ActiveRecord::Base
has_many :address
end
=============================================================================Address
Controller
=============================================================================
class AddressController < ApplicationController
scaffold :address
def edit
@address = Address.find(@params["id"])
end
end
=============================================================================Table
descriptions
=============================================================================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 | |
+-----------------+------------------+------+-----+---------+----------------+
mysql> desc states;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | | PRI | NULL | auto_increment |
| state_cd | char(2) | | | | |
+----------+------------------+------+-----+---------+----------------+
> class Address < ActiveRecord::Base > has_and_belongs_to_many :person, :join_table => "person_addresses" > belongs_to :location > belongs_to :state > end > > class State < ActiveRecord::Base > has_many :address > endSince you''re not specifically overriding the auto-pluralization, should those be pluralized associations like this: has_and_belongs_to_many :people belongs_to :locations belongs_to :states and has_many :addresses
amith-sCgbW8QO9uUAvxtiuMwx3w@public.gmane.org
2005-Feb-07 19:33 UTC
re: collection_select help...
Quoting Andrew Otwell <andrew-uQjPo4GTFqgS+FvcfC7Uqw@public.gmane.org>:>> class Address < ActiveRecord::Base >> has_and_belongs_to_many :person, :join_table => "person_addresses" >> belongs_to :location >> belongs_to :state >> end >> >> class State < ActiveRecord::Base >> has_many :address >> end > > Since you''re not specifically overriding the auto-pluralization, should > those be pluralized associations like this: > > has_and_belongs_to_many :people > belongs_to :locations > belongs_to :states > > and > > has_many :addressesI''m not sure... Based on the documentation it says I should use the singular form http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html (i''m looking at the following example) Both express a 1-1 relationship, the difference is mostly where to place the foreign key, which goes on the table for the class saying belongs_to. Example: class Post < ActiveRecord::Base has_one :author end class Author < ActiveRecord::Base belongs_to :post end The tables for these classes could look something like: CREATE TABLE posts ( id int(11) NOT NULL auto_increment, title varchar default NULL, PRIMARY KEY (id) ) CREATE TABLE authors ( id int(11) NOT NULL auto_increment, post_id int(11) default NULL, name varchar default NULL, PRIMARY KEY (id) ) I also tried pluralizing all the table names in my models (like you suggested) and that did not seem to help. I still get two options "selected" (and neither of them are the right one!). Thanks Amith
I don''t know if this has anything to do with the problem, but addresses.state_id (char(2)) seems to be of different type than states.id (int(11)). This might cause some weird and hard-to-track problems. //jarkko> 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 | > | > +-----------------+------------------+------+-----+--------- > +----------------+ > > > mysql> desc states; > +----------+------------------+------+-----+---------+----------------+ > | Field | Type | Null | Key | Default | Extra | > +----------+------------------+------+-----+---------+----------------+ > | id | int(11) unsigned | | PRI | NULL | auto_increment | > | state_cd | char(2) | | | | | > +----------+------------------+------+-----+---------+----------------+ > >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
amith-sCgbW8QO9uUAvxtiuMwx3w@public.gmane.org
2005-Feb-07 20:47 UTC
Re: collection_select help...
Quoting Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org>:> I don''t know if this has anything to do with the problem, but > addresses.state_id (char(2)) seems to be of different type than > states.id (int(11)). This might cause some weird and hard-to-track > problems.This was it. I forgot to change the column type when I split the state out into another table. Thanks also to Andrew Otwell for his help on my pluralization mistakes. Thanks Amith