Ruby on Rails 3:将数组与数据库顺序匹配

发布于 2024-12-09 14:56:39 字数 176 浏览 0 评论 0原文

我有一个页面,上面有一些图像,但并不总是填充所有字段。由于我使用数组来填充每个字段,因此它们的顺序正确很重要。我在“页面”和“事物”之间有一个连接表,称为“网格”来跟踪顺序。

如何使用连接表的顺序构造数组?

数据库: 页面通过网格有很多东西 事物通过网格有很多页面

感谢您提供的任何帮助!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光匆匆的小流年 2024-12-16 14:56:39

您可以使用 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")

忆伤 2024-12-16 14:56:39

谢谢!我已经这样做了,问题是有些字段是空的。想象一个有 6 个字段的空白页面,可以填充这些字段。如果我现在更改字段号。 4、然后填充字段的数组将把它放在字段 1 中。

因此,如果我有一个页面,有 6 个字段,并且只填充了其中一个字段,

the array should look like this: 
    array[0] = blank
    array[1] = blank
    array[2] = blank
    array[3] = blank
    array[4] = object from @things where the order = 4 
    array[5] = blank

    Right now it looks like this: 
    array[0] = object from @things where the order = 4 

谢谢

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,

the array should look like this: 
    array[0] = blank
    array[1] = blank
    array[2] = blank
    array[3] = blank
    array[4] = object from @things where the order = 4 
    array[5] = blank

    Right now it looks like this: 
    array[0] = object from @things where the order = 4 

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文