Skip to content

Instantly share code, notes, and snippets.

@gouvermxt
Created May 2, 2011 20:06
Show Gist options
  • Save gouvermxt/952257 to your computer and use it in GitHub Desktop.
Save gouvermxt/952257 to your computer and use it in GitHub Desktop.
class ObjectGroup < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :object_items
strip_attributes!
validates :description, :presence => true, :uniqueness => true, :allow_blank => true
#Search
def self.search(search)
# ObjectGroup.find_by_sql('SELECT DISTINCT object_groups.* FROM object_groups_object_items INNER JOIN object_items ON object_items.id = object_groups_object_items.object_item_id RIGHT JOIN object_groups ON object_groups.id = object_groups_object_items.object_group_id')
# ObjectGroup.find(:all, :include => [:object_items, :users], :conditions => "object_groups.description LIKE '%#{search}%' OR object_items.description LIKE '%#{search}%' OR users.name LIKE '%#{search}%'", :order => 'object_groups.created_at DESC')
end
def to_s
description
end
end
class ObjectGroupsController < ApplicationController
layout 'admin/admin'
# GET /object_groups
# GET /object_groups.xml
def index
@object_groups = ObjectGroup.all.paginate(:page => params[:page], :per_page => 20, :order => 'created_at DESC ')
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @object_groups }
end
end
# GET /object_groups/1
# GET /object_groups/1.xml
def show
@object_group = ObjectGroup.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @object_group }
end
end
# GET /object_groups/new
# GET /object_groups/new.xml
def new
@object_group = ObjectGroup.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @object_group }
end
end
# GET /object_groups/1/edit
def edit
@object_group = ObjectGroup.find(params[:id])
end
# POST /object_groups
# POST /object_groups.xml
def create
@object_group = ObjectGroup.new(params[:object_group])
respond_to do |format|
if @object_group.save
format.html { redirect_to(@object_group, :notice => t('form.object_group.messages.created')) }
format.xml { render :xml => @object_group, :status => :created, :location => @object_group }
else
format.html { render :action => "new" }
format.xml { render :xml => @object_group.errors, :status => :unprocessable_entity }
end
end
end
# PUT /object_groups/1
# PUT /object_groups/1.xml
def update
@object_group = ObjectGroup.find(params[:id])
respond_to do |format|
if @object_group.update_attributes(params[:object_group])
format.html { redirect_to(@object_group, :notice => t('form.object_group.messages.updated')) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @object_group.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /object_groups/1
# DELETE /object_groups/1.xml
def destroy
@object_group = ObjectGroup.find(params[:id])
@object_group.destroy
respond_to do |format|
format.html { redirect_to(object_groups_url, :notice => t('form.object_group.messages.destroyed')) }
format.xml { head :ok }
end
end
def search
@search = {
:results => ObjectGroup.search(params[:search]).paginate(:page => params[:page], :per_page => 20, :order => 'created_at DESC '),
:term => params[:search]
}
respond_to do |format|
format.html
format.xml
end
end
end
require 'strip_attributes'
class ObjectItem < ActiveRecord::Base
has_and_belongs_to_many :object_groups
strip_attributes!
validates :description, :presence => true, :uniqueness => true
def self.search(search)
ObjectItem.order('object_items.created_at DESC').includes(:object_groups).order('object_groups.description DESC').where("object_items.description LIKE '%#{search}%' OR object_groups.description LIKE '%#{search}%'")
# ObjectItem.find(:all, :include => :object_groups, :conditions => "object_items.description LIKE '%#{search}%' OR object_groups.description LIKE '%#{search}%'", :order => 'object_items.created_at DESC')
end
def to_s
description
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment