I have a design model, which allows designers to upload a design they have made. I would like to query for designs with the same title, uploaded by the same designer and group them together.
I can’t get my head around on how do I do this in. Do you have a suggestion?
I run Rails 3.2, Ruby 1.9.3 and Mongoid.
More info:
Designers model:
class Designer
has_many :designs, :order => "created_at"
end
Designs model:
class Design
belongs_to :designer
attr_accessible :image, :tags, :description, :title, :featured
mount_uploader :image, DesignerUploader
field :width
field :height
field :description
field :title
field :tags, type: Array
field :featured, :type => Boolean, :default => false
end
Designs controller:
def newest
@designs = Design.select("title, count(title) as title_count").group(:title).having("title_count > 1")
end
I have a designer who can upload a lot of designs. If he uploads designs with the same title, they should be grouped together (I’m thinking the best way for this would be to create an array inside an array). I don’t know how to query for this in the database. Any ideas?
EDIT
I think I need something similar to this:
Design.find(:all, :group => [:title], :having => "count(*) > 1" )
This should give me a single instance of all the duplicates titles. When I have that, I can query for those titles and put them in a nested array or something like that.
Problem is that I don’t know how you can make this query in mongoid?!
Your models are significantly incomplete, so please accept the following working solution as an attempt to remain close to your models, and the answer as just a suggestion for moving ahead.
config/routes.rb
test/unit/designer_test.rb
test/functional/designs_controller_test.rb
result