nested

Automatically add parent model id in nested resources

孤人 提交于 2019-12-06 07:12:29
问题 With nested resource routes in Rails 3, such as the following: resources :magazines do resources :ads end helpers such as magazine_ad_path are defined, to which I have to pass both a magazine and the ad, which is inconvenient if I just have a reference to the ad: magazine_ad_path(@ad.magazine, @ad) Is there a nice way to set up an ad_path helper that takes the @ad and returns the appropriate address including the magazine ID? (This would also then allow the use of link_to @ad , redirect_to

C++ Nested Scope Accessing

左心房为你撑大大i 提交于 2019-12-06 07:03:22
问题 I recently saw this code in cppreference: string str="global scope"; void main() { string str="main scope"; if (true){ string str="if scope"; cout << str << endl; } cout << str << endl; } Which outputs: if scope main scope This is fine, I understand the whole nested scope thing, and I know that the 'str' inside the if scope will be destroyed when the stack unwinds it at the end of the statement, so it wont be available after that, hence the second print takes the main 'str' as its argument.

Ruby. Merging a nested hash without overwriting

六眼飞鱼酱① 提交于 2019-12-06 06:55:20
I have a nested hash: { ["X", 1, 2, 3]=> { ["X", "O", 2, 3]=> { ["X", "O", "X", 3]=>["X", "O", "X", "O"] } } } I want to merge a given nested hash: { ["X", 1, 2, 3]=> { ["X", "O", 2, 3]=> { ["X", "O", 2, "X"] => ["X", "O", "O", "X"] } } } such that: { ["X", 1, 2, 3]=> { ["X", "O", 2, 3]=> { ["X", "O", "X", 3]=>["X", "O", "X", "O"], ["X", "O", 2, "X"] => ["X", "O", "O", "X"] } } } What's the best way? The hashes I'll be merging will have an equivalent key at an arbitrary depth of nested-ness. The value of the last nested hash will always be different from all the other hashes. If you're sure

C++: Nested map

依然范特西╮ 提交于 2019-12-06 06:31:41
问题 Here is the definition: struct nmap; struct nmap: map<string, boost::variant<string, nmap*>>{}; The last line below doesn't work: nmap my_map; my_map["a"] = "b"; my_map["c"] = new nmap; my_map["c"]["d"] = "e"; What do I need to add, in order for this to work? 回答1: I'd suggest either going for a tiny little readable helper: #include <boost/variant.hpp> #include <map> using std::map; struct nmap; struct nmap: map<std::string, boost::variant<std::string, nmap*>> { typedef boost::variant<std:

Find a key inside a deeply nested dictionary

∥☆過路亽.° 提交于 2019-12-06 06:00:46
I have a lot of nested dictionaries, I am trying to find a certain key nested inside somewhere. e.g. this key is called "fruit". How do I find the value of this key? @Håvard's recursive solution is probably going to be OK... unless the level of nesting is too high, and then you get a RuntimeError: maximum recursion depth exceeded . To remedy that, you can use the usual technique for recursion removal: keep your own stack of items to examine (as a list that's under your control). I.e.: def find_key_nonrecursive(adict, key): stack = [adict] while stack: d = stack.pop() if key in d: return d[key]

Compounded relative font-sizes: a clean way to adopt the font-size of the child, not the parent element

谁都会走 提交于 2019-12-06 05:50:00
For example, if I have: td { font-size: 1.3em } h2 { font-size: 2em } h3 { font-size: 1.6em } p { font-size: 1.2 } And I have headings/paragraphs inside my table-cells, I know that I can avoid compounding the font-sizes by the following: td h2, td h3, td p { font-size: 1em } Which would result in the headings/paragraphs in my table-cells having font-size of 1.3em (that of the td). But what I'm looking for is a nice, clean way for each child element to have it's original font-size, not that of the parent. I'd really like to avoid doing the following (and of course I'd like to avoid using px):

Should I refactor static nested classes in Java into separate classes?

守給你的承諾、 提交于 2019-12-06 05:47:20
I have inherited code which contains static nested classes as: public class Foo { // Foo fields and functions // ... private static class SGroup { private static Map<Integer, SGroup> idMap = new HashMap<Integer, SGroup>(); public SGroup(int id, String type) { // ... } } } From reading SO (e.g. Java inner class and static nested class ) I believe that this is equivalent to two separate classes in two separate files: public class Foo { // Foo fields and functions // ... } and public class SGroup { static Map<Integer, SGroup> idMap = new HashMap<Integer, SGroup>(); public SGroup(int id, String

Is there a way to get the function a decorator has wrapped?

我只是一个虾纸丫 提交于 2019-12-06 05:13:42
问题 Suppose I have @someDecorator def func(): '''this function does something''' print 1 Now, the object func is an instance of someDecorator . Is there some way I can access the function it holds, i.e something like func.getInnerFunction() . For instance, if I need to retrieve the doc string of func() . 回答1: See functools.wraps: http://docs.python.org/library/functools.html. The decorator gets the name and doc string of the original function. You use it like this: def decorator(f): @functools

Haskell - Does a replace function exist?

折月煮酒 提交于 2019-12-06 05:12:06
问题 I have to make three functions for replacing of flat strings and in lists. I don't know, whether there is a replace function like in other languages. I searched for that however unfortunately without success :-( So my attempt is yet quite thin. 1st function: replace :: String -> String -> String -> String replace findStr replaceStr myText = replace()?? My approach for the 1st function: replace :: String -> String -> String -> String replace [] old new = [] replace str old new = loop str where

Rails 4 nested attributes multiple records when updating

吃可爱长大的小学妹 提交于 2019-12-06 05:09:31
问题 I'm stuck and i don't know why it is not working right. I have a model product wich has many tags. When i update the product rails update properly the products attributes but is creating another tag record instead of just updating it. here is my code : View form: <%= form_for ([@product.user, @product]), id: 'edit_form' do |f| %> <%= render 'shared/error_messages', object: f.object %> <div class="field"> <%= f.label :name %><br> <%= f.text_field :name %> </div> <div class="field"> <%= f.label