Dev Purkayastha
2005-Mar-30 23:56 UTC
Extending a has_and_belongs_to_many -- does that make sense?
I''ve found myself creating lots of new objects that belong to 2 others, whereas I was almost wishing to merely extend my has_and_belongs_to_many table. That is, if I have Paintings and Colors, these has_and_belong_to_many to each other. But, what if I want to describe the *intensity* of each color in a painting? One one hand, I can make a model (ColorSplotch), of which Paintaings and Colors have many. But I can also imagine extending the colors_paintings table with an ''intensity'' field. Is that any rails way of doing so with ActiveRecord?
Rick Olson
2005-Mar-31 00:19 UTC
Re: Extending a has_and_belongs_to_many -- does that make sense?
> I''ve found myself creating lots of new objects that belong to 2 > others, whereas I was almost wishing to merely extend my > has_and_belongs_to_many table. > > That is, if I have Paintings and Colors, these has_and_belong_to_many > to each other. But, what if I want to describe the *intensity* of each > color in a painting? One one hand, I can make a model (ColorSplotch), > of which Paintaings and Colors have many. But I can also imagine > extending the colors_paintings table with an ''intensity'' field. Is > that any rails way of doing so with ActiveRecord?Yup! Create your Painting and Color models with the appropriate has_and_belongs_to_many association. In the colors_paintings table, add your intensity field. When adding colors you can simply do this: @painting = Painting.find 13 @color = Color.find 1 @color.intensity = 50 @painting.colors << @color Now, to retrieve them: <% @painting.colors.each do |color| %> <%=color.intensity%> <% end %> -- rick http://techno-weenie.net
Matthew Thill
2005-Mar-31 00:21 UTC
Re: Extending a has_and_belongs_to_many -- does that make sense?
Your last suggestion seems to be the easier way. ActiveRecord has the ability to return attributes on the join table, and provides a method called push_with_attributes when you specify a has_and_belongs_to_many relationship. ActiveRecord::Assiciations::ClassMethods has the documentation on it. http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html Dev Purkayastha wrote:> I''ve found myself creating lots of new objects that belong to 2 > others, whereas I was almost wishing to merely extend my > has_and_belongs_to_many table. > > That is, if I have Paintings and Colors, these has_and_belong_to_many > to each other. But, what if I want to describe the *intensity* of each > color in a painting? One one hand, I can make a model (ColorSplotch), > of which Paintaings and Colors have many. But I can also imagine > extending the colors_paintings table with an ''intensity'' field. Is > that any rails way of doing so with ActiveRecord? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Pedro Santos
2005-Mar-31 09:24 UTC
Re: Extending a has_and_belongs_to_many -- does that make sense?
I would be very cautious with this solution. It happens that to my knowledge ActiveRecord has no way to update the attributes in the join table. At least nobody answered the questions that have popped up about this problem. This means that after setting the relation between Painting and Color and defining the ''intensity'', you cannot change it! By the way, I make here an appeal to the rails gurus out there. I am not a professional developer, and I donĀ“t know enough of ActiveRecord to do it. But it seems to me that it would relatively easy to create a method update_attributes(obj, attrib_hash) to update attributes in join tables. Best regards, Pedro on 05/03/31 1:19, Rick Olson at technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:>> I''ve found myself creating lots of new objects that belong to 2 >> others, whereas I was almost wishing to merely extend my >> has_and_belongs_to_many table. >> >> That is, if I have Paintings and Colors, these has_and_belong_to_many >> to each other. But, what if I want to describe the *intensity* of each >> color in a painting? One one hand, I can make a model (ColorSplotch), >> of which Paintaings and Colors have many. But I can also imagine >> extending the colors_paintings table with an ''intensity'' field. Is >> that any rails way of doing so with ActiveRecord? > > Yup! Create your Painting and Color models with the appropriate > has_and_belongs_to_many association. In the colors_paintings table, > add your intensity field. When adding colors you can simply do this: > > @painting = Painting.find 13 > @color = Color.find 1 > @color.intensity = 50 > @painting.colors << @color > > Now, to retrieve them: > > <% @painting.colors.each do |color| %> > <%=color.intensity%> > <% end %>