2015-09-02 110 views
1

我有一個對象通過bridge-object ContactSector聯繫has_many個扇區。我在聯繫人索引頁上有一個表單,我現在用簡單的字符串過濾聯繫人列表。Rails:用另一個對象(has_many到)過濾對象列表

我想有一個複選框列表的部門,@contacts只返回與選定的扇區的聯繫人列表。它不必匹配所有的人,至少一個。

模式

class Contact < ActiveRecord::Base 
    include Filterable 

    scope :firstname, -> (firstname) { where("firstname like ?", "#{firstname}%")} 
    scope :lastname, -> (lastname) { where("lastname like ?", "#{lastname}%")} 
    scope :title, -> (title) { where("title like ?", "#{title}%")} 
    scope :sectors, -> (sector) {where("sector like ?", "#{sector}%")} #Attempt 

    has_many :sectors, through: :contact_sectors 
    has_many :contact_sectors 

    accepts_nested_attributes_for :sectors 
end 

class ContactSector < ActiveRecord::Base 
    belongs_to :contact 
    belongs_to :sector 
end 

class Sector < ActiveRecord::Base 
    has_many :contacts, :through => :contact_sectors 
    has_many :contact_sectors 
end 

filterable.rb

module Filterable 
    extend ActiveSupport::Concern 

    module ClassMethods 
    def filter(filtering_params) 
     results = self.where(nil) 
     filtering_params.each do |key, value| 
     results = results.public_send(key, value) if value.present? 
     end 
     results 
    end 
    end 
end 

查看

<%= form_tag '', :method => :get do %> 
    <label>Sectors</label><br> 
    <% Sector.all.each do |sector| %> 
     <%= hidden_field_tag 'sector_ids[]', '' %> 
     <%= check_box_tag 'sector_ids[]', sector.id %> 
     <%= sector.sector %> 
    <% end %> 
<% end %> 

控制器

def index 
    @contacts = Contact.filter(params.slice(:firstname, sector_ids: []) 
end 

回答

0

型號

class Sector < ActiveRecord::Base 
    has_many :contacts, :through => :contact_sectors 
    has_many :contact_sectors 
    def name_with_initial 
     "#{sector}" 
    end 
end 

查看

<%= collection_check_boxes(:sector, :ids, Sector.all, :id, :name_with_initial) do |b| %> 
    <%= b.label do %> 
     <%= b.check_box %> 
     <%= b.text %> 
    <% end %> 
<% end %> 

控制器

def index 
    @contacts = Contact.filter(params.slice(:firstname) #Or just Contact.all 
    @filter = params[:sector][:ids] 
    @filter.shift #First array entry is always a blank string 
    unless @filter.to_a.empty? 
     @contacts = @contacts.includes(:sectors).references(:sectors).where('sectors.id IN (?)', @filter) 
    end 
end 

我做多個排序與各種類別的列表,所以我使用提供的字符串數組由該函數來迭代濾波器:

def filtercontacts(filters) 
    filters.each { 
     |x| 
     if eval('params[:' + x + ']')!=nil 
     @filter = eval('params[:'+ x +'][:ids]') 
     @filter.shift 
     end 
     unless @filter.to_a.empty? 
     @contacts = @contacts.includes(eval(':'+x.pluralize)).references(eval(':'+x.pluralize)).where(eval('\''+ x.pluralize+'.id IN (?)\''), @filter) 
     @filter = [] 
     end 
    } 
    @contacts 
    end 

然後使用像這樣

@contacts = filtercontacts(Array['area', 'art', 'category', 'demographic', 'donation_type', 'education', 'project', 'sector', 'support']) 
相關問題