【发布时间】:2014-09-05 15:53:44
【问题描述】:
假设我有两个类,如下所示:
# has attribute name
class Foo
accepts_nested_attributes_for :bar
end
# has attribute bing
class Bar
end
是否有可能在 Rails 中以某种方式轻松消除调用属于嵌套类的属性的需要(在本例中为 Bar)。也就是可以做到以下几点吗?
my_foo.name
my_foo.bing
我需要为Foo 中的每个Bar 属性创建实例方法吗?
此外,是否可以创建以这种方式设置的新关系,而无需传入显式参数哈希?
my_foo = Foo.new(name: "my_foo_name", bing: "my_foo.bar.bing")
提前感谢您对此的任何帮助
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 model attributes nested-attributes