ransack_alias 未在查询中执行任何操作
ransack_alias not doing anything in query
我将 Ransack 用于我的搜索表单,并且我正在尝试使用 ransack_alias
来缩短查询。但是,当我执行搜索时,别名字段根本不会过滤查询。我已将我的表单简化为只有一个简单的别名,但它似乎不起作用。
用户模型
class User < ActiveRecord::Base
ransack_alias :ec, :email_cont
end
index.html.haml
= search_form_for @q, url: users_path, html: {method: :get} do |f|
= f.label :ec
= f.search_field :ec
= f.submit 'Search'
如果我在控制器中放置一个断点:
>> User.count
10
>> User.ransack({email_cont: "test@email.com"}).result.count
(93.8ms) SELECT COUNT(*) FROM "users" WHERE ("users"."email" ILIKE '%test@email\.com%')
1
>> params[:q]
{"ec" => "test@email.com"}
>> User.ransack(params[:q]).result.count
(28.4ms) SELECT COUNT(*) FROM "users"
10
>> User._ransack_aliases
{"ec" => "email_cont"}
我做错了什么?
ransack_alias
用于别名属性,但不是比较键。
class User < ActiveRecord::Base
ransack_alias :e, :email
end
> User.ransack({'e_cont' => "user"}).result.count
(0.2ms) SELECT COUNT(*) FROM "users" WHERE ("users"."email" LIKE '%user%')
我将 Ransack 用于我的搜索表单,并且我正在尝试使用 ransack_alias
来缩短查询。但是,当我执行搜索时,别名字段根本不会过滤查询。我已将我的表单简化为只有一个简单的别名,但它似乎不起作用。
用户模型
class User < ActiveRecord::Base
ransack_alias :ec, :email_cont
end
index.html.haml
= search_form_for @q, url: users_path, html: {method: :get} do |f|
= f.label :ec
= f.search_field :ec
= f.submit 'Search'
如果我在控制器中放置一个断点:
>> User.count
10
>> User.ransack({email_cont: "test@email.com"}).result.count
(93.8ms) SELECT COUNT(*) FROM "users" WHERE ("users"."email" ILIKE '%test@email\.com%')
1
>> params[:q]
{"ec" => "test@email.com"}
>> User.ransack(params[:q]).result.count
(28.4ms) SELECT COUNT(*) FROM "users"
10
>> User._ransack_aliases
{"ec" => "email_cont"}
我做错了什么?
ransack_alias
用于别名属性,但不是比较键。
class User < ActiveRecord::Base
ransack_alias :e, :email
end
> User.ransack({'e_cont' => "user"}).result.count
(0.2ms) SELECT COUNT(*) FROM "users" WHERE ("users"."email" LIKE '%user%')