Patrick A. wrote:
MereMortal wrote:
another question:
is it also the case that in Ruby, you just design the relational tables
in an RDBMS (say like MySQL0 and Ruby turns them automatically into an
object hierarchy? is that all you have to do?
yes, if you use a plugin such as DRYSQL. If you don’t you’ll need to
specify has_many and co relation stuff in your models.
You answered the high-level question.
The low-level question is this. If you have a database with a table in
it call People, and its primary key is called ‘id’, and you have a
config/database.yml file pointing to it, then this is all the Ruby you
must write to access a record in the table
class Person < ActiveRecord::Base
end
That gives you several million convenience functions to access any
Person by any attribute, and upgrade & write that Person back.
p = Person.find(42)
p.name = “Forsythe”
p.save!
The system reads the People schema and builds all the elements of the
Person object automatically. Oh, and it also changes the number of the
nouns, where appropriate.
Next, you might want to “refactor” your database. That’s typically
hard, because customer data must survive upgrades. Rails provides a
complete “migration” system to let you express database changes as
high-level commands, such as rename_column or add_column.
Next, if you have relations in your database, you can match them with
Rails relations:
class Person < ActiveRecord::Base
has_many :pets
end
That pulls in another table, Pets, and matches the person_id in each
pet with the id of each Person. That provides lines like…
dogs = person.pets.find_by_species(‘dog’)
Patrick answered the question “why doesn’t the schema reader also read
the relations and automatically set all the has_many and such
directives correctly”. Default Rails doesn’t do that because you
typically want a minor subset of all the relations.
–
Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!