繁体   English   中英

Rails-Ransack-通过关联进行搜索

[英]Rails - Ransack - search through associations

用户has_many个人资料和个人资料has_many电子邮件地址。 我有“用户”搜索表,并且有“电子邮件”字段。 我该如何实现。

form_for @User.search do |U|
 f.text_field :name, :name_cont
 f.text_field :email, #what should I write here?

我不确定这是否是完美的方法。 您可以尝试添加一个

has_many :email, through: profiles用户模型中的has_many :email, through: profiles 完成此操作后,您可能要做的就是:user_emails_cont

也可以尝试:user_profiles_emails_cont ,不要有很多直通关系。

模型中的示例覆盖:

ransacker :devices_count, formatter: proc{ |v|
    data = ContractSection.csa.includes(:contract_section_devices)
    .where("
            (select count(*) from contract_section_devices 
            where contract_section_devices.contract_section_id = contract_sections.id) = #{v.to_i}
    ").map(&:id)
  } do |parent|
parent.table[:id]

结束

视图::devices_count_in

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM