【问题标题】:Rails_admin filtering with associations problemsRails_admin 过滤关联问题
【发布时间】:2012-08-29 15:44:41
【问题描述】:

我在确定如何在 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

集线器模型:

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

【问题讨论】:

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


    【解决方案1】:

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

    searchable :organization
    

    应该是

    searchable :name
    

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

    【讨论】:

    • 在我的情况下,我必须在 searchable :name 下明确指定 queryable true 以使其通过快速“过滤器”输入进行搜索(尽管 wiki 说此行为默认设置为 true)
    • 这对我来说也是如此。上面的评论为我节省了数小时的研究时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    相关资源
    最近更新 更多