Need two indexes on a HABTM join table?

Close – you most likely want the following: add_index :person_products, [:person_id, :product_id], :unique => true add_index :person_products, :product_id The :unique => true is not strictly required and it depends whether or not it makes sense to have a person associated with a product multiple times. I would say if you’re not sure, you probably do … Read more

Insert element in Python list after every nth element

I’ve got two one liners. Given: >>> letters = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’] Use enumerate to get index, add ‘x’ every 3rd letter, eg: mod(n, 3) == 2, then concatenate into string and list() it. >>> list(”.join(l + ‘x’ * (n % 3 == 2) for n, l in enumerate(letters))) [‘a’, ‘b’, ‘c’, ‘x’, ‘d’, ‘e’, ‘f’, ‘x’, … Read more