使用belongs_to关联的整数字段在RailsAdmin中过滤

时间:2014-09-29 16:32:57

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

我试图配置"字段搜索"在我的User模型上使用RailsAdmin和belongs_to关联,名为Budget

class User < ActiveRecord::Base
  belongs_to :budget
end

class Budget < ActiveRecord::Base
  has_many :users
end

Budget对象的字段price_in_dollars类型为Integer

在RailsAdmin中,查看Budget对象,我可以点击&#34;添加过滤器&#34;并选择&#34; Price in dollar&#34;,它允许使用&#34; Number&#34;进行过滤。或&#34;介于...和...&#34;之间,指定范围。

我想在User模型上使用类似的过滤,使用Budget列按price_in_dollars进行过滤。

RailsAdmin documentation on List(在&#34;字段搜索&#34;中)部分说明:

  

Belongs_to association:如果label不是虚拟的(:name,:title等),将在其foreign_key(:team_id)或其标签上进行搜索,您还可以在目标表或源表上指定列

基于此,我将此块添加到我的User模型中:

rails_admin do
  list do
    field :budget do
      searchable [:price_in_dollars]
    end
  end
end

通过这个块,我现在可以输入一个值,例如&#34; 500&#34;并使用&#34;过滤&#34;或&#34;正好是&#34;,并使用price_in_dollars列,而不是外键ID。

但是,我无法按范围进行过滤,就像直接转到Budget管理页面时一样。

有没有办法强制过滤菜单使用特定于数字或整数的选项?它似乎可能将其视为关于过滤的字符串数据类型。

1 个答案:

答案 0 :(得分:0)

您可以像这种方式搜索一个Emirates_to关联的整数值

class PagerViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIGestureRecognizerDelegate  {

@objc func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

let colors: [UIColor] = [
    UIColor.blue,
    UIColor.red,
    UIColor.yellow,
    UIColor.green
]
var vcs: [UIViewController] {
    var vcList: [UIViewController] = []
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    for i in 0...3 {
        let vc = storyboard.instantiateViewController(withIdentifier: "ui")
        (vc as? UIColViewController)?.vcIndex = i
        vc.view.backgroundColor = colors[i]
        (vc as? UIColViewController)?.panGesture.delegate = self
        vcList.append(vc)
    }
    return vcList
}


func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = (viewController as? UIColViewController)?.vcIndex else { return nil }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else { return vcs.last }

    guard vcs.count > previousIndex else { return nil }

    return vcs[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    guard let viewControllerIndex = (viewController as? UIColViewController)?.vcIndex else { return nil }

    let nextIndex = viewControllerIndex + 1

    guard nextIndex < vcs.count else { return vcs.first }

    guard vcs.count > nextIndex else { return nil }

    return vcs[nextIndex]
}

override func viewDidLoad() {
    self.delegate = self
    self.dataSource = self
    if let first = vcs.first {
        setViewControllers([first], direction: .forward, animated: true, completion: nil)
    }
}