Skip to content

Instantly share code, notes, and snippets.

@zimuliu
zimuliu / nested_attributes_sync.rb
Last active June 30, 2021 12:37
#accepts_nested_attributes_sync_for: Instead of manually specifying _destroy: true for deletion, or id for update, it supports synchronizing an association, with respect to the given unique index in the association.
module Concerns
module NestedAttributesSync
extend ActiveSupport::Concern
included do
class_attribute :nested_attributes_sync_options, instance_writer: false
self.nested_attributes_sync_options = {}
end
class_methods do
@zimuliu
zimuliu / gist:9820d110a10d264726ac
Last active August 29, 2015 14:09
countUnique
#include <iostream>
#include <set>
#include <vector>
int countUnique(const std::vector<std::string> &a) {
std::set<std::string> uniqueStrings;
for (std::vector<std::string>::const_iterator itr = a.begin(); itr != a.end(); itr++) {
if (uniqueStrings.end() == uniqueStrings.find(*itr)) {
uniqueStrings.insert(*itr);
}
@zimuliu
zimuliu / gist:6dcd1878d85b94972075
Created November 18, 2014 19:56
Break in nested loops
bool all_done = false;
for (int i = 0; i < 100; i ++) {
for (int j = 0; j < 100; j ++) {
if (i * j == 81) {
all_done = true;
break;
}
}
if (all_done) {