美文网首页Ruby & RailsRubyRuby on Rails
Using ransack and delegate in Ra

Using ransack and delegate in Ra

作者: 老码农不上班 | 来源:发表于2017-09-09 23:40 被阅读42次

Personal page

I have used Rails to develop web project for two years, But it's the first time to use ransack, a Gem base on Object searing. This note recording my experience of using it for my current work.

Background

Five months ago, I join in a starup company in HangZhou, which provides SASS software for car dealer. our guys cooperate with each other remotely, Sometime, our developer guys would customize software for car dealer. Fuck the outsourcing. These day, we modify our software to fix the needs for a famous car industry enterprise in China.

Back for program

Rails app provide Restful API in backend. In common case, index action gets the resources, we would filter list by some condition, for example, Cars#index gets car list according to brand name and other relationship model. If your data is huge, Object-base searing is not suitable for your case, your should consider to use Elasticsearch.

In our API, car model hash_one qiyuan_detetion_report,every report has a state attribute. Now, I want to cars from Car#index, which associate detection reports state is true.

# car model
has_one :qiyuan_detection_report
...
delegate :state, to: :qiyuan_detection_report, allow_nil: true, prefix: true

Firstly, Let's get familiar with delegate in Rails. We can delegate state attribute in qiyuan_detection_report model for car model, so car.qiyuan_detection_report_state is the same as car.qiyuan_detection_report.state.

go ahead is about ransack, the basic usage is skiped, if your don't know what is ransack, I suggest you read it's docutment hear.

To filter true state detection_report cars
fronted-end just needs passing params[:query][:qiyuan_detection_report_state_eq] = verified key value to Car#index action
and backend also simple too: car_relation_object.ransack(params[:query]).result

finally is our rspec code fragment.

it "query by delegate qiyuan_detection_report state" do
  auth_get :index, query: {
    qiyuan_detection_report_state_eq: "verified"
  }
  expect(response_json.fetch(:data).count).to eq(1)
end

相关文章

  • Using ransack and delegate in Ra

    Personal page I have used Rails to develop web project fo...

  • airbnb 房源预定 embedding 论文学习

    《Real-time Personalization using Embeddings for Search Ra...

  • gem Ransack

    Ransack Ransack 是一个支持搜索和排序的 gem。可以在这里看到详细介绍。 安装 然后 使用 Vie...

  • ruby on rails的一些小坑

    坑一 写了一个页面的查询,用的是ransack. 这个是一个多态关联.在controller中使用ransack查...

  • gem ransack 的一些搜索条件

    gem ransack详细介绍 html: 控制器:

  • gem 'ransack'

    使用: params 使用:q 代替:search 使用search_form_for而不是form_for 需要...

  • gem ransack介绍

    最近开始接触内网,刚开始觉得畅言网代码挺乱的,但是跟内网比起来真是小巫见大巫,看过内网后,感觉畅言网太清爽了,瞬间...

  • RA

    RA是一种无监督排序方法,适用于底层排序,主要优势有三点:1. 成本低2. 随机抽取样本,更符合数据真实分布。3....

  • delegate的调用方法总结

    delegate的调用方法总结 1、直接调用:delegate( args )或者delegate.Invoke(...

  • sqlsugar where条件

    using Dapper; using SqlSugar; using SyntacticSugar; using...

网友评论

  • lanzhiheng:cool article。I plan to use rails or jekyll to build my personal blog in the near feature。:smile:
    lanzhiheng:@hww_面条酱 我也是15毕业 不是大哥
    老码农不上班:Just do it, my big brother.

本文标题:Using ransack and delegate in Ra

本文链接:https://www.haomeiwen.com/subject/shtzjxtx.html