I'm using acts_as_taggable_on and I want to get a list of unique taggings. How can I do this? In a more general sense, say that I have a Product model with attributes name and price. I have these: id: 1 Name: Juice Price: 5 id: 2 Name: Juice Price: 5 So when I do a Product.find(:all) I'll get both, but I only want one. How can I do this so that no matter now many "duplicates" there are, I'll only get one PLUS all the other UNIQUE products?
on 2009-02-27 09:21
on 2009-02-27 17:25
Mike Chai wrote: > I'm using acts_as_taggable_on and I want to get a list of unique > taggings. How can I do this? In a more general sense, say that I have > a Product model with attributes name and price. I have these: > > id: 1 > Name: Juice > Price: 5 > > id: 2 > Name: Juice > Price: 5 > > So when I do a Product.find(:all) I'll get both, but I only want one. > How can I do this so that no matter now many "duplicates" there are, > I'll only get one PLUS all the other UNIQUE products? I'm confused. How does this relate to acts_as_taggable_on? What I see here is that you have a products table that allows the product name to be duplicated. This sounds like a bad idea to me. It seems more logical to either validate that the product name is unique with a unique index in the database to ensure the name is always unique. Or identify your products by something other than the name (like maybe SKU Number or Item Number). Something exposed to the user that they can use to find the right product. Product.find(:all) is supposed to give you both. That's what it does. If you must insist on doing a distinct select then you'll have to drop down to a lower level like... Product.find_by_sql("SELECT DISTINCT name, price from products....") But, again this seems like a really bad idea because you have no idea which one you're going to get and if you included the id in the distinct select then you would still get both anyway. With all that said, I think you need to reevaluate your application's design.
on 2009-02-27 18:12
Thanks for the help. That was just an example, not a good one, but I just needed to explain the problem I'm having. It had nothing to do with acts_as_taggable_on, but I might as well just directly tell my problem. I want to display all the tags that a tagger has used. However, using @user.owned_taggings returns ALL the taggings, including duplicates. What I mean by duplicates, is that the taggable_type and the tag_id are the same. That means if someone used one tag on many different Products, it would be a list of that one word. On Feb 27, 8:25 am, Robert Walker <rails-mailing-l...@andreas-s.net>