I don''t know if I have missed something in the documentation, but I have been trying to create a relationship between a couple of models that I can only describe as a "contains" relationship. The simplified objects are: themes - id INT - name VARCHAR ... - main_color_id INT - secondary_color_id INT colors - id INT - red INT - green INT - blue INT I would like to create a relationship between parent.green_child_id -> child.id and parent.blue_child_id -> child.id such that I would be able to use the expression "theme.main_color.red" and "theme.secondary_color.red". Is that currently possible or would there need to be a new kind of relationship created? thanks. e.
Hello, I think you can just do it like this (not tested): class Themes belongs_to :main_color, :class_name => "Color" belongs_to :secondary_color, :class_name => "Color" . . . end Not really sure what you wanted to say with: <snip> I would like to create a relationship between parent.green_child_id -> child.id and parent.blue_child_id -> child.id </snip> Hope it helps :-) On 9/6/05, eric lindvall <eric@5stops.com> wrote:> I don't know if I have missed something in the documentation, but I have > been trying to create a relationship between a couple of models that I can > only describe as a "contains" relationship. > > The simplified objects are: > > themes > - id INT > - name VARCHAR > ... > - main_color_id INT > - secondary_color_id INT > > colors > - id INT > - red INT > - green INT > - blue INT > > I would like to create a relationship between > parent.green_child_id -> child.id and > parent.blue_child_id -> child.id > > such that I would be able to use the expression "theme.main_color.red" > and "theme.secondary_color.red". > > Is that currently possible or would there need to be a new kind of > relationship created? > > thanks. > > e. > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hello Eric, eric lindvall said the following on 2005-09-06 03:48:> The simplified objects are: > > themes > - id INT > - name VARCHAR > ... > - main_color_id INT > - secondary_color_id INT > > colors > - id INT > - red INT > - green INT > - blue INTThis is a has_one relationship in Rails. So, class Theme has_one :main_color, :class_name => ''Color'' has_one :secondary_color, :class_name => ''Color'' end class Color end ActiveRecord::Associations::ClassMethods#has_one http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000428 Hope that helps ! François
> Not really sure what you wanted to say with: > <snip> > I would like to create a relationship between > parent.green_child_id -> child.id and > parent.blue_child_id -> child.id > </snip>Yeah.. it was late and I changed the example half way through writing. It should have been themes.main_color_id -> colors.id themes.secondary_color_id -> colors.id That''s funny that you would use a ''belongs_to'' relationship for something that ''contains'' something else. I tried using a ''has_on'' relationship but the problem with thas is it expects to find a column ''colors.theme_id'' which does not exist (and doesn''t make sense, because there is more than one linkage between ''themes'' and ''colors''). If I use a ''belongs_to'', it seems that I am not able to say: theme.main_color.red = 5 which seems to make sense with the documentation. I''ve also tried theme.create_main_color(:red => 5) which creates the entry in the ''colors'' table, but does not assign the ''colors.id'' to ''themes.main_color_id''. (I see the UPDATE happen on the ''themes'' table, but main_color_id is always set to NULL) Am I still doing something wrong? thanks. e. On Tue, 06 Sep 2005, pepe wrote:> Hello, > > I think you can just do it like this (not tested): > > class Themes > belongs_to :main_color, :class_name => "Color" > belongs_to :secondary_color, :class_name => "Color" > . > . > . > end