开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜