I have a table that is a "has one" relationship with another table. Table A is the parent table, Table B is the child table. Instead of Table B having a table_b_id field to accomplish the relationship, Table A has a table_b_id field that points to the primary key in table_b. How do I describe this relationship in Rails? has_one doesn''t work, so I wasn''t sure what else to look at. Thanks -- http://www.approachingnormal.com _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
The table with the othertable_id column on it should have belongs_to :othertable othertable should have has_one. In your example: If Table A is "houses", and Table B is "roofs": Your "houses" table has a roof_id column. "roofs" just has an id. house.rb: belongs_to :roof roof.rb: has_one :house It sounds to me like you''ve got the "_id" column on the wrong table, but not knowing what the two models are, I can''t say for sure. --Wilson. On 10/7/05, Larry Wright <larrywright-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a table that is a "has one" relationship with another table. Table A > is the parent table, Table B is the child table. Instead of Table B having a > table_b_id field to accomplish the relationship, Table A has a table_b_id > field that points to the primary key in table_b. > > How do I describe this relationship in Rails? has_one doesn''t work, so I > wasn''t sure what else to look at. > > Thanks > > -- > http://www.approachingnormal.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 10/7/05, Larry Wright <larrywright@gmail.com> wrote:> I have a table that is a "has one" relationship with another table. Table A > is the parent table, Table B is the child table. Instead of Table B having a > table_b_id field to accomplish the relationship, Table A has a table_b_id > field that points to the primary key in table_b. > > How do I describe this relationship in Rails? has_one doesn't work, so I > wasn't sure what else to look at.You're probably looking for belongs_to. With belongs_to the foreign key lives in table A. In the API docs there's pretty good in-depth coverage of this, under ActiveRecord::Associations::ClassMethods. It's still sometimes hard to wrap your head around it, though. - Rowan -- Morality is usually taught by the immoral. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails