如何根据属性过滤索引操作?

时间:2015-08-22 21:13:15

标签: ruby-on-rails ruby-on-rails-4

如何创建路由及其相应的控制器方法,以根据属性过滤索引操作内容?

我当前的路线.rb有:

resources :parties
resources :organizations, controller: 'parties', type: 'Organization' 
resources :people, controller: 'parties', type: 'Person' 

当我只需要过滤客户方时,我会运行查询:

Party.joins(:party_relationships).where(party_relationships: {relation:"customer"})

如何创建路由让控制器知道我必须仅过滤客户方,同时保持我的路线RESTful?

提前致谢,

此致 Leandro的

有关当前型号的更多信息:

class Party < ActiveRecord::Base
   has_many :party_relationships
   has_many :second_parties, :through => :party_relationships
   scope :organizations, -> { where(type: 'Organization') } 
   scope :people, -> { where(type: 'Person') } 

    def self.types
      %w(Organization Person)
    end
end

组织

class Organization < Party
end

class Person < Party
end

会员的关系

class PartyRelationship < ActiveRecord::Base
  belongs_to :party, :class_name=>'Party'
  belongs_to :second_party, :class_name => 'Party'
  enum relation: [ :customer, :supplier, :employee ]
end

0 个答案:

没有答案