如何创建具有多个依赖于同一个父级的关联的工厂?
父模型:
class Parent < ActiveRecord::Base
has_many :codes
has_many :parent_filters
validates :parent, :presence => true, :uniqueness => true
end
Run Code Online (Sandbox Code Playgroud)
菲特勒模型:
class Filter < ActiveRecord::base
has_many :parent_filters
validates :filter, :presence => true, :uniqueness => true
end
Run Code Online (Sandbox Code Playgroud)
ParentFilter 连接模型:
class ParentFilter < ActiveRecord
belongs_to :parent
belongs_to :filter
validates :filter, :presence => true
validates :parent, :filter, :presence => true, :uniqueness => [ :scope => filter ]
end
Run Code Online (Sandbox Code Playgroud)
AdhocAttribute 模型:
class AdhocAttribute < ActiveRecord::Base
has_many :adhoc_mappings
has_many :codes, :through => :adhoc_mappings
has_many :parent_filters, :through …Run Code Online (Sandbox Code Playgroud) 大多数在线资料都有这样的初始化:
class MyClass
attr_accessors :a, :b, :c
def initialize(a,b,c)
@a = a
@b = b
@c = c
end
end
Run Code Online (Sandbox Code Playgroud)
有或没有默认值.创建一个新实例是:
n = MyClass.new(1,2,3)
n.a # => 1
n.b # => 2
n.c # => 3
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用哈希语法初始化实例,如:
n = MyClass.new(:a => 1, :b => 2, :c => 3)
Run Code Online (Sandbox Code Playgroud)
这相当于:
n = MyClass.new(:b => 2, :a => 1, :c => 3)
Run Code Online (Sandbox Code Playgroud)
这难以实施吗?