我有性病是这样的:如何将STI类重新分配给模型方法中的变量?
class Post
end
class Post::Confirmed < Post
end
class Post::Draft < Post
def confirm!
becomes Post::Confirmed
end
end
...# somewhere in controller
# POST /posts/1/confirm
# POST /posts/1/confirm.json
def confirm
@post = Post::Draft.first
@post = @post.confirm! # this is the only way I can reload @post with Post::Confrmed
end
是它在某种程度上可能使:
@post.confirm! # I want this @post(Post::Draft) to become Post::Confirmed without reassigning
还是仅仅也不回报率呢?
在此先感谢!
你确定STI在这种情况下是否合适?不会有简单的状态标志和一些范围更有意义吗? – 2014-10-02 21:21:10
我虽然这样做,但范围有点大,然后在这个例子中:所有的子类作为给定类的命名空间,所以草案有自己的方法和确认的职位 - 他们自己的。 我想我可能是错的。但是,STI看起来仍然很完美。 – IlyaDoroshin 2014-10-02 21:21:46
@ muistooshort想象一下,你有购物车。它在结构上与订单相同:它有物品,总价格等,但每个类的方法都不同。 – IlyaDoroshin 2014-10-02 21:25:19