Ruby on Rails 3:将数组与数据库顺序匹配
我有一个页面,上面有一些图像,但并不总是填充所有字段。由于我使用数组来填充每个字段,因此它们的顺序正确很重要。我在“页面”和“事物”之间有一个连接表,称为“网格”来跟踪顺序。
如何使用连接表的顺序构造数组?
数据库: 页面通过网格有很多东西 事物通过网格有很多页面
感谢您提供的任何帮助!
I have a page, that has some images on them, but it's not always that all the fields are populated. Since I am using an array to populate each field, it matters that they are in the right order. I have a join table between "page" and "thing", called "grid" to keep track of the order.
How can I construct an array using the join table's order ?
Database:
Page has many Things through Grids
Thing has many Pages through Grids
Thanks for any help you can provide!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 ActiveRecord::QueryMethods 中定义的 order(*args),如下所示
@page.things.order("grids.order asc")
You can use order(*args) defined in ActiveRecord::QueryMethods like this
@page.things.order("grids.order asc")
谢谢!我已经这样做了,问题是有些字段是空的。想象一个有 6 个字段的空白页面,可以填充这些字段。如果我现在更改字段号。 4、然后填充字段的数组将把它放在字段 1 中。
因此,如果我有一个页面,有 6 个字段,并且只填充了其中一个字段,
谢谢
Thanks! I already did that, the problem is that some of the field are empty. Imagine a blank page with 6 fields, that can be populated. If I now change field no. 4, then the array that populates the fields, will put it in field no 1.
So if I have a page, with 6 fields, and only one of them is populated,
Thanks