The Neurochild wrote:
> Hi...
> 
> I want to know the equivalent instructions to insert, update, search
> and delete a registry. I know Rails can do it easily Thanks to
> ActiveRecord, but here's the catch: I'm using PostgreSQL. As I'm using
> SQL to do the migration (Including the foreign keys), I need to know
> if I can use SQL queries to do those actions. Is there an example out
> there?
> 
> I uncommented this line in environment.rb:
> config.active_record.schema_format = :sql
> 
> See you around!
> 
> The Neurochild

You can use raw SQL in migrations via the execute() method. But you can 
also use ActiveRecord objects in migrations, so you can take advantage 
of all the goodness that AR gives you. As for foreign keys, you can 
write yourself a helper to do that in a migration-friendly way. The 
syntax that I use for PostgreSQL is this:

def foreign_key(foreign_table, foreign_column, primary_table, 
primary_column = :id)
  execute "
    alter table #{foreign_table.to_s}
      add constraint fk_#{foreign_table.to_s}_#{foreign_column.to_s}
      foreign key (#{foreign_column.to_s}) references 
#{primary_table.to_s} (#{primary_column.to_s})
  "
end

def delete_foreign_key(foreign_table, foreign_column)
  execute "alter table #{foreign_table.to_s} drop constraint 
fk_#{foreign_table.to_s}_#{foreign_column.to_s}"
end

Put those methods in a file that gets loaded and use them like

foreign_key :orders, :customer_id, :customers

and

delete_foreign_key :orders, :customer_id

Peace.
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to