Rails_admin过滤与关联问题

时间:2012-09-04 23:35:13

标签: ruby ruby-on-rails-3 ruby-on-rails-3.1 rubygems rails-admin

我在查找如何在列表视图中搜索Rails_Admin界面中的模型时遇到问题。

背景资料:

  • 模型:帖子,用户,集线器和组织。
  • 目标:用户使用通过集线器设备将http请求发布到数据库的设备发布帖子。数据库显示用户的帖子。帖子属于用户,该用户通过属于集线器的设备发送帖子,该集线器属于组织。
  • 我想要完成的任务:在rails admin中,我希望Post模型的自定义字段显示制作帖子的用户所属的组织。
  • 当前问题:我可以通过使用Post模型的组织方法将Post的用户组织显示为字段中的字符串,但是我无法在Rails_admin中创建可以使其可搜索的过滤器。在Rails_Admin中,将行searchable :organization添加到config.model Post do...field :organization do{}end...end不起作用。

我真的很感激有关此事的任何建议。谢谢!

发布模型:

class Post < ActiveRecord::Base

  belongs_to :user
  belongs_to :hub

  attr_accessible :hub_id, :user_id

  before_validation :set_count

  validates :hub_id, :presence => true
  validates :user_id, :presence => true
  validates :count, :presence => true, :numericality => {:greater_than_or_equal_to => 0}

  before_create :update_redis

  def organization
    self.hub.organization.name
  end

  def user_email
    self.user.email
  end

  private

  def user_email
    self.user.email
  end

end

用户模型:

class User < ActiveRecord::Base

  belongs_to :organization
  has_many :posts, :extend => User::Posts

  accepts_nested_attributes_for :organization

  attr_accessible :email, :password, :password_confirmation, :remember_me, :as => [:default, :admin]
  attr_accessible :device_id, :organization_id, :role_ids, :as => :admin
  attr_accessible :organization_attributes, :as => [:admin, :manager]

  after_save :update_total

end

Hub模型:

class Hub < ActiveRecord::Base

  devise :token_authenticatable, :trackable

  belongs_to :organization, :inverse_of => :hubs
  has_many :posts

  attr_accessible :location, :organization_id, :as => :admin

  validates :organization, :presence => true
  validates :location, :presence => true

  before_save :ensure_authentication_token

end

组织模式:

class Organization < ActiveRecord::Base

  attr_accessible :name, :street_address_1, :street_address_2, :city, :state, :zip_code, :goal_per_hour, :as => [:admin, :manager
  ]
  has_many :hubs
  has_many :posts, :through => :hubs, :extend => Organization::Posts
  has_many :users

  validates :name, :presence => true, :uniqueness => true

end


rails_admin.rb初始值设定项:

config.model Post do
  list do
    filters [:user, :hub]
    items_per_page 200
    field :id do
      column_width 50
    end
    field :count do
      column_width 35
    end
    field :user do
      column_width 50
    end
    field :user_email
    field :hub do
      column_width 50
    end
    field :organization do
      label "Organization"
      # searchable :organization # This line doesn't work!!
    end
    field :created_at do
      strftime_format "%m/%d/%y %H:%M"
      column_width 90
    end
    #field :updated_at do
    #  strftime_format "%m/%d/%y %H:%M"
    #  column_width 90
    #end
  end
  show do
    include_all_fields
    field :user_email
    field :organization_name, :string do
      label "Organization"
    end
  end
end

1 个答案:

答案 0 :(得分:4)

看起来记录的答案确实有效 - 您的行

searchable :organization

应该是

searchable :name

这将为组织(在“添加过滤器”菜单下)添加一个搜索名称字段的选项,但不会在该字段上添加对快速“过滤器”输入的搜索。