Technology is supposed to be fun ...

11/12 2007

Posted: November 12th, 2007 | Author: | Filed under: .bash_history, All, Ruby on Rails | Tags: , , , | No Comments »

One cool thing I learned today was that you can set the columns when you generate the rails migration from the terminal. The prepopulates the tabel generation code with the table names and datatype. This isn’t such a big find since it’s well documented in script/generate model—help. But here is how it looks.


 script/generate model members name:string age:integer
 script/generate model portfolio member_id:integer body:text

Another thing I just came to realize is that defining explicit foreign_key constraint to the database is probably a good idea. I don’t know too much about database administration but if I understand it correctly it helps with dataintegrity and probably performance if the table grows big. Adding indexes is also a plus. A little specifity never hurt anyone. This example is for MYSQL with the InnoDB engine which I believe is the default for rails now.


. . .
def self.up
create_table :members do |t|
t.column :name, :string
t.column :age, :integer
end
end
. . .

def self.up
create_table :portfolios do |t|
t.column :member_id, :integer
t.column :body, :text
end
execute 'ALTER TABLE portfolios ADD CONSTRAINT fk_portfolios_member \\
FOREIGN KEY (member_id) REFERENCES members(id)'
end
. . .

			 

Comments are closed.