我有一个复杂的模型,并希望在我对 Rails 的有限了解的情况下获得完整的功能。
我有一个部分、一个标题(使用acts_as_tree)和一个项目。
我使用 json 来引入数据集。这非常有效。我希望能够为整组数据(例如“is_shippable”)引入属性。我希望能够在树中的任何位置指定 is_shippable 值并将其设置为 true。另外,我希望能够在标题或项目级别覆盖以将其设置为 false。
我已经决定将 is_shippable 作为部分、标题和项目的属性并尝试使用 before_create 回调来确定它是否应该是 is_shippable 是有意义的。
例如:
section
header -acts_as_tree
item - is_shippable
示例 json:
{
"name":"sample section",
"is_shippable": true,
"headers_attributes":[
{
"name":"sample_section"
"items_attributes":[{
"name":"sample item",
"is_shippable":false,
}
]
}
]
}
在 header.rb 中
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
在 item.rb 中
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
有没有比我更好的方法来做到这一点?如果在层次结构中将 is_shippable 设置为 true,我将如何执行 if 语句检查 is_shippable 是否设置为 false?
编辑 - 还有更多 is_shippable 功能,如 is_fragile、is_custom_size 等...