arr = [
{id: 23, title: 'aaa', parent_id: nil},
{id: 24, title: 'aab', parent_id: 23},
{id: 25, title: 'aac', parent_id: 23},
{id: 26, title: 'aad', parent_id: 25},
{id: 27, title: 'aae', parent_id: 25},
{id: 28, title: 'aaf', parent_id: 27},
{id: 29, title: 'aag', parent_id: 27},
{id: 30, title: 'aah', parent_id: 24},
{id: 31, title: 'aaz', parent_id: 24},
]
new_arr = []
get_child = -> (item) do
find_result = arr.select { |x| x[:parent_id] == item[:id] }
if find_result.any?
arr = arr - find_result
new_arr += find_result
find_result.each do |x|
get_child.call(x)
end
end
end
get_child.call({id: 25, parent_id: 23})
puts new_arr
content_tag :ul, class: 'fancy' do
hashtags.map(&:to_s).map do |hashtag|
# XXX transitional
url = h.hashtag_page_enabled? && h.logged_out? ? h.tag_path(hashtag.downcase) : h.hashtag_friends_path(q: hashtag.downcase)
content_tag :li do
link_to "##{hashtag}", url, dir: h.html_dir(hashtag)
end
end.join.html_safe
end
environment.plugins.set(
'Provide',
new webpack.ProvidePlugin({
...
Bloodhound: 'typeahead.js/dist/bloodhound.js' // полный путь к вашей библиотеке
})
)
class Place
include Mongoid::Document
field :current_position, type: Boolean, default: "false"
validates :user_id, presence: true
belongs_to :user, touch: true
after_save :update_position_for_other, if: :current_position_changed?
private
def update_position_for_other
Place.where(:id.ne => id, user_id: user_id).update_all(current_position: false) if current_position
end
end
sorted_ratings = movies.each_with_object({}) do |(name, votes), hash|
sum = votes.values.sum
hash[name] = sum.nonzero? ? sum.to_f / votes.count : 0
end.sort_by(&:last).reverse
sorted_ratings.each do |name, rating|
if rating.nonzero?
puts "#{name} is rated #{rating}"
else
puts "Rating is not available for #{name}"
end
end
# app/constraints/version_constraint.rb
class VersionConstraint
def initialize
@versions = %w[ v1 v2 ]
end
def matches?(request)
@versions.include? request.path_parameters[:version].to_s
end
end
#routes.rb
scope ':version', constraints: VersionConstraint.new do
concerns :versioned_api_methods
end