具有复合主键的 Rails 3.1 嵌套资源 (id +parent_id)
假设我需要嵌套在routes.rb中的两个资源,如下所示:
resources :post do
resources :comment
end
按照约定,comments.id将是评论主键和comments.post_id 将是外键。
我需要将主键作为复合键[comments.post_id, comments.id]。
这样我就可以拥有每个不同帖子的第一个评论,id == 1,每个不同帖子的第二个评论,id == 2,依此类推...
当然,我还需要禁用引用评论的每个路由(子资源),而不同时引用其帖子(父资源)。
这只是一个例子,我的实际项目不是关于博客的(我以不同的方式处理这个问题),我想知道是否有一种方法可以实现嵌套资源的这种行为以实现兼容性与遗留数据库。
谢谢。
Let's assume I need two resources nested in the routes.rb as follows:
resources :post do
resources :comment
end
By convention comments.id will be the comments primary key and comments.post_id will be a foreign key.
I need the primary key to be the composite key [comments.post_id, comments.id].
So that I could have every first comment of each distinct post with id == 1, every second comment of each distinct post with id == 2 and so on...
Of course I also need to disable every route that refers to a comment (the child resource) without referring also to its post (the parent resource).
This is just an example, my actual project isn't about a blog (I'd handled this problem differently), I'd like to know if there is a way to achieve such a behavior for a nested resource in order to achieve compatibility with a legacy database.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法是创建另一列(保留 posts.id 作为主键),在该列上添加唯一性验证,其范围为帖子 id,然后在 _create hood 之前或之后写入一些内容以生成该列值。
一个示例(不是真正的代码)
因此 sub_id 列充当主键。当您查询某个帖子的评论时,您可以执行以下操作:
或
请注意,应调整此处的实际逻辑以满足您的要求。例如,如果可以删除评论,那么在帖子上保留一个计数器可能是个好主意,该计数器将用于确定下一个 sub_id (或编写一个 sub id 生成器类)。
One way to do this is by creating another column (leave the posts.id as it was primary key), add an uniqueness validation on that column with scope to the post id, then write some before or after _create hood to generate that column value.
An example (not real code)
So the sub_id column acts as a primary key. When you query a comment for a certain post, you do this:
or
Please be noted the real logic here should be adjusted to satisfy your requirement. For instance, if comments can be deleted, it might be a good idea to keep an counter on post which will used to determine the next sub_id (or write a sub id generator class).
实际上我不太确定你想要完成什么以及为什么?也许你冷得更清楚一点。无论如何,两个链接可能会有所帮助:
Rails 中的复合主键、
Rails 关联
所以,如果可以的话,我将使用第三个模型来实现它,如第二个模型中所述上面的链接。如果这是不可能的,您可能想尝试第一个链接中提到的 gem。
Actually I am not quite sure what you are trying to accomplish and why? Maybe you cold make that a bit clearer. Anyway two links which might help:
Composite Primary Keys in Rails,
Rails Associations
So, if you can I would implement that using a third model as explained in the second link above. If thats is not possible, you might want to try them gem mentioned in the first link.
只是旁注:可能是
before_create 的逻辑
this.sub_id = post.comments.size + 1
结束
应该通过删除评论的适当处理来支持。否则我们很快就会遇到重复的 sub_ids。
Just a side note: Probably logic a la
before_create do
this.sub_id = post.comments.size + 1
end
should be backed up by suitable handling of deleting comments. Otherwise we would soon run into duplicate sub_ids.