3. Searching ActiveRecord...
寄弌曳^、何蛍匯崑は誼吭じゃない
Issue.where("id < ?", 3).to_sql
#=> "SELECT `issues`.* FROM `issues` WHERE (id < 3)"
Issue.where("name LIKE ?", '%hoge%').to_sql
#=> "SELECT `issues`.* FROM `issues` WHERE (name LIKE '%hoge%')"
仭くなってきた´
7. What is Ransack?
ransack gem
https://github.com/activerecord-hackery/ransack
4 ActiveRecordをして碧C嬬をつけるgem
4 O協なしで匯宥りの碧をカバ`
4 high googleability ★ googleトップがgithub
4 ドキュメントが樋
8. How to use Ransack
In your Gemfile:
gem 'ransack'
> bundle install
Rails4, 4.1喘に恷m晒されたbranchもある
9. Extend ActiveRecord
>bundle exec rails g model Issue name:string
>bundle exec rake db:migrate
class Issue < ActiveRecord::Base
end
で
Issue.search(name_cont: 'hoge').result.to_sql
#=> "SELECT `issues`.* FROM `issues`
# WHERE (`issues`.`name` LIKE '%hoge%')"
12. Predicates
eq, gt, gteq, lt, lteq, in, cont, start, end and more
https://github.com/activerecord-hackery/ransack/
wiki/Basic-Searching
13. Alias method
class Issue < ActiveRecord::Base
def self.search
'hoge'
end
end
Issue.search
#=> 'hoge'
Issue.ransack(name_cont: 'hoge').result.to_sql
#=> "SELECT `issues`.* FROM `issues`
# WHERE (`issues`.`name` LIKE '%hoge%')"
24. Define ransacker
4 2つのカラムの、鮟Y栽したものと曳^
class Address < ActiveRecord::Base
ransacker :address do |parent|
Arel::Nodes::NamedFunction.new(
'CONCAT', [parent.table[:region],parent.table[:city]]
)
end
end
Address.search(address_cont: '寄昜軒寄昜偏')
#=> "SELECT `addresses`.* FROM `addresses`
# WHERE (CONCAT(`addresses`.`region`, '', `addresses`.`city`)
# LIKE '%寄昜軒寄昜偏%')"
25. Define ransacker
4 磯叔スペ`スをoした碧
ransacker :name_without_spaces, formatter: proc { |v| v.gsub(' ', '') } do |parent|
Arel::Nodes::NamedFunction.new(
'REPLACE', [parent.table[:name], ' ', '']
)
end
Issue.search(name_without_spaces_eq: 'h o g e').result.to_sql
#=> "SELECT `issues`.* FROM `issues`
# WHERE REPLACE(`issues`.`name`, ' ', '') = 'hoge'"