Has_and_belongs_to_many and order by

hey,

this is my situation, and i want to fetch all documents of a certain
product, ordered by index_col

class Product < ActiveRecord::Base
has_and_belongs_to_many :fsdocuments
end

class Fsdocument < ActiveRecord::Base
has_and_belongs_to_many :products
end

table fsdocuments
id int(11) Nee auto_increment
filename varchar(250) utf8_general_ci Nee
filetype varchar(20) utf8_general_ci Nee
filesize bigint(20) Nee 0
name varchar(250) utf8_general_ci Nee
description varchar(250) utf8_general_ci Nee
lang varchar(10) utf8_general_ci Nee
index_col int(11) Nee 0
created_at datetime Nee 0000-00-00 00:00:00
publish tinyint(1) Nee 0
publish_from date Ja 0000-00-00
publish_to date Ja 0000-00-00
created_by int(11) Ja NULL
updated_at datetime Nee 0000-00-00 00:00:00
updated_by int(11) Ja NULL
deleted_at datetime

table fsdocuments_products
fsdocument_id int(11) Nee 0
product_id int(11) Nee 0

table products
id int(11) Nee auto_increment
name_en varchar(100) utf8_general_ci Nee
name_fr varchar(100) utf8_general_ci Nee
name_nl varchar(100) utf8_general_ci Nee
price decimal(10,2) Nee 0.00
publish tinyint(1) Nee 0
publish_from date Ja 0000-00-00
publish_to date Ja 0000-00-00
created_at datetime Nee 0000-00-00 00:00:00
created_by int(11) Ja NULL
updated_at datetime Nee 0000-00-00 00:00:00
updated_by int(11) Ja NULL
deleted_at datetime

when i fetch documents of a certain product i do this:
docs = product.fsdocuments

now i want those document direct ordered by the “index_col” column,
smallest first. so i get 1 2 3 4 5 6 …

Can anyone help me?

Thx

Perhaps this might work:

class Product < ActiveRecord::Base
has_and_belongs_to_many :fsdocuments, :order => ‘index_col’
end

On 9/1/06, Nick B. [email protected] wrote:

class Fsdocument < ActiveRecord::Base
lang varchar(10) utf8_general_ci Nee
table fsdocuments_products
publish_from date Ja 0000-00-00
now i want those document direct ordered by the “index_col” column,


http://www.bdcsoftware.com" - Automotive CRM

Rafael S. wrote:

Perhaps this might work:

class Product < ActiveRecord::Base
has_and_belongs_to_many :fsdocuments, :order => ‘index_col’
end

On 9/1/06, Nick B. [email protected] wrote:

class Fsdocument < ActiveRecord::Base
lang varchar(10) utf8_general_ci Nee
table fsdocuments_products
publish_from date Ja 0000-00-00
now i want those document direct ordered by the “index_col” column,


http://www.bdcsoftware.com" - Automotive CRM

it works fine

thx btw :wink: