Note that there are some explanatory texts on larger screens.

plurals
  1. POacts_as_tree and propogating a value through sub headers to items
    text
    copied!<p>I have a little bit of a complex model and want to get the full functionality capable with my limited understanding of rails. </p> <p>I have a section, a header (which uses acts_as_tree), and an item. </p> <p>I use json to bring sets of data in. This has worked very well. I would like to be able to bring attributes in for whole sets of data such as 'is_shippable'. I would like to be able to specify anywhere in the tree the is_shippable value and set to true. Also, I would like to be able to override at the header or item level to set it to false. </p> <p>I have decided that it makes sense to have is_shippable as an attribute on the section, header, and item and try to use a before_create callback to determine whether it should be is_shippable.</p> <p>For example: </p> <pre><code>section header -acts_as_tree item - is_shippable </code></pre> <p>sample json: </p> <pre><code>{ "name":"sample section", "is_shippable": true, "headers_attributes":[ { "name":"sample_section" "items_attributes":[{ "name":"sample item", "is_shippable":false, } ] } ] </code></pre> <p>} </p> <p>in header.rb </p> <pre><code>before_save :default_values private def default_values self.is_shippable ||=self.section.is_shippable # need to be able to set header to is_shippable=false if specified explicitly at that level end </code></pre> <p>in item.rb </p> <pre><code>before_save :default_values private def default_values # if not set, default to 0 self.is_shippable ||= 0 self.is_shippable=1 if self.header.is_shippable==true # need to be able to set item to is_shippable=false if specified explicitly at that level end </code></pre> <p>Is there a better way to do this than I am doing? How would I execute in if statements checking if is_shippable is set to false if it has been set to true higher in the hieararchy?</p> <p>thx</p> <p>EDIT - also there are more features that is_shippable like is_fragile, is_custom_size etc...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload