开发者

Ruby on rails 3: Matching an array to a database order

开发者 https://www.devze.com 2023-04-13 01:54 出处:网络
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 ri

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!


You can use order(*args) defined in ActiveRecord::QueryMethods like this

@page.things.order("grids.order asc")


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号