Skip to content

Instantly share code, notes, and snippets.

@AlxGolubev
AlxGolubev / association.md
Last active April 7, 2017 17:25
Способ загрузки ассоциаций при использовании find_by_sql

При выполнении сложных запросов, часто бывает необходимо выполнить предзагрузку данных об ассоциациях какой либо модели. Обычно сложные запросы выполняються через find_by_sql, например

Post.find_by_sql "SELECT p.name, c.author FROM posts p, comments c WHERE p.id = c.post_id"
> [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...]

Чтобы при вывзове ассоциаций объектов избежать проблемы с select(n+1) запросами в Rails существует eager-loading, например:

@AlxGolubev
AlxGolubev / user_recent_media
Created May 6, 2015 16:24
User Recent Media result (methods)
Enumerable#methods:
all? detect each_slice entries find_all group_by lazy max_by min_by none? reduce slice_when to_set
chunk each_cons each_with_index exclude? flat_map index_by many? member? minmax one? slice_after sort_by
collect_concat each_entry each_with_object find grep inject max min minmax_by partition slice_before sum
JSON::Ext::Generator::GeneratorMethods::Array#methods: to_json_without_active_support_encoder
V8::Conversion::Array#methods: to_v8
Columnize#methods: columnize columnize_opts columnize_opts=
Array#methods:
& as_json concat empty? fourth keep_if product rindex size to_a to_xml
* assoc count eql? from last push rotate slice to_ary transpose
@AlxGolubev
AlxGolubev / libv8
Last active August 29, 2015 14:19
Installing libv8 gem on OS X 10.9+
`bundle config build.libv8 --with-system-v8`
then run
`bundle install`