20100302

ruby on rails and reorder the columns order in the view

This year was quite difficult to me, as I don't have a job since the 4th of January until now (2nd of March). And I think that is going to be like that for a some more time (as the interesting projects that I was offered to work in, the money is not enough, but that projects are really cool).

Well, this weekend I tried to keep going into my cook book, now in ruby, as the previous version in Perl was quite nice, but nobody would take that serious, as I did everything by myself and with my own XML definitions. The model that now I'm working is quite easy, a simple database:



ActiveRecord::Schema.define(:version => 20100302024114) do

create_table "categories", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "ingredients", :force => true do |t|
t.string "name"
t.integer "recipe_id"
t.integer "quantity"
t.string "measure"
t.string "part"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "notes", :force => true do |t|
t.integer "recipe_id"
t.string "keyword"
t.text "text"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "recipes", :force => true do |t|
t.string "name"
t.integer "yield"
t.integer "category_id"
t.text "preparation"
t.datetime "created_at"
t.datetime "updated_at"
end

end



This is quite simple, but yet powerful enough to what I need. For example, if i ever need something like the origin of the recipe, I just could add a note titled "origin" and the region in the text field. The only problem and that I should reconsider in some time, is that some preparations have parts, for example the base, the filling and the cover (for example a lemon pie). I manage to put that in the ingredients, as some parts uses the same ingredients, but in different measures, and perhaps somebody just want to make only the filling or the cover, but not the whole meal. But having another table for the preparation, just I don't like for now (as the preparations could vary from 1 to 5 or more, that should be in a separated table).

The main problem that I had was that the documentation was quite nasty about the reorder of the columns. Every web that I found was telling how to reorder the rows (the information in the columns) by an specified column. But I want to reorder the column, and the formal documentation just don't say anything useful to me.

So, after digging a lot of time in Google, I found http://api.rubyonrails.org/ and inside that http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002294 That was the beginning of the solution.

I printed that method, the code was:

class RecipesController < ApplicationController

active_scaffold :recipe do |config|
p config.columns()
end

end


That was quite useful, the very first line of the output was:


Completed in 301ms (View: 26, DB: 0) | 200 OK [http://10.0.0.52/recipes/new?_method=get&adapter=_list_inline_adapter]
#<ActiveScaffold::DataStructures::Columns:0x7fa930f14100 @_inheritable=[:name, :yield, :category, :ingredients, :preparation, :notes],
[...]


For the first time I had the proper names to call the others tables and my recipe table without the default order.


config.columns = [:name, :yield, :category, :ingredients, :preparation, :notes]


I know that this post was quite long and in this nasty language, but most of the people (well, programmers) just search the things in English.
Publicar un comentario