【问题标题】:Active Admin use related model fields in filters活动管理员在过滤器中使用相关模型字段
【发布时间】:2016-12-19 15:05:13
【问题描述】:

我有用户模型和地址,SocialProfile 模型。我想在活动管理员的用户资源的过滤器部分中显示地址模型的字段,如“zip”、“address_line_1”。

作为地址模型,我想在用户的同一资源中显示 SocialProfile 模型的字段。 如何在 belongs_to 模型的活动管理员中显示下拉和文本搜索中的字段?

class User < ActiveRecord::Base
  has_many :addresses
  has_many :social_profiles
end

class Address < ActiveRecord::Base
 belongs_to :user
end

class SocialProfile < ActiveRecord::Base
  belongs_to :user
end

app/admin/user.rb 中用户资源的过滤器

filter :mobile
filter :full_name
filter :zip #to use the address model's "zip" field
filter :source #use social_profile model's "social" field           

【问题讨论】:

    标签: ruby-on-rails activeadmin


    【解决方案1】:

    您应该能够通过使用过滤关联模型

    filter :address_zip, as: :string
    filter :social_profile_mobile, as: :string 
    

    它只会过滤关联模型的那些特定字段。我可能不知道哪些属性属于哪个模型,但想法是一样的。

    【讨论】:

    • 太棒了!!为我工作! filter :addresses_zip, as: :string filter :social_profiles_mobile, as: :string
    • 如果我有 Document 模型并且它属于属于 Document 的 User 和 IdentityDocument 模型,它也可以工作,因此在 Document AA Resource 我可以添加过滤器,如 Document belongs_to User filter :user_mobile, as: :string Document has_many IdentityDocument,其中type是IdentityDocument的一个属性 filter :identity_documents_type, as: :string
    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    相关资源
    最近更新 更多