Is there a way to get a collection of all the Models in your Rails app?

The whole answer for Rails 3, 4 and 5 is: If cache_classes is off (by default it’s off in development, but on in production): Rails.application.eager_load! Then: ActiveRecord::Base.descendants This makes sure all models in your application, regardless of where they are, are loaded, and any gems you are using which provide models are also loaded. This … Read more

Clone a List, Map or Set in Dart

Use of clone() in Java is tricky and questionable1,2. Effectively, clone() is a copy constructor and for that, the Dart List, Map and Set types each have a named constructor named .from() that perform a shallow copy; e.g. given these declarations Map<String, int> numMoons, moreMoons; numMoons = const <String,int>{ ‘Mars’ : 2, ‘Jupiter’ : 27 … Read more

How to copy Java Collections list

b has a capacity of 3, but a size of 0. The fact that ArrayList has some sort of buffer capacity is an implementation detail – it’s not part of the List interface, so Collections.copy(List, List) doesn’t use it. It would be ugly for it to special-case ArrayList. As tddmonkey has indicated, using the ArrayList … Read more

Identify duplicates in a List

The method add of Set returns a boolean whether a value already exists (true if it does not exist, false if it already exists, see Set documentation). So just iterate through all the values: public Set<Integer> findDuplicates(List<Integer> listContainingDuplicates) { final Set<Integer> setToReturn = new HashSet<>(); final Set<Integer> set1 = new HashSet<>(); for (Integer yourInt : … Read more

Best way to create an empty map in Java

1) If the Map can be immutable: Collections.emptyMap() // or, in some cases: Collections.<String, String>emptyMap() You’ll have to use the latter sometimes when the compiler cannot automatically figure out what kind of Map is needed (this is called type inference). For example, consider a method declared like this: public void foobar(Map<String, String> map){ … } … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)