James W. wrote:
I have looked at many rails apps source code from the web and it seems
there is no best practice when designing the database schema and
models.
I’m not sure that’s actually the case. Database best-practices are
database best-practices. This, IMO, has nothing to do with Ruby, Rails
or Rails model classes.
For example surely it is recommended to include your foreign keys and
constraints at the database level? Some people just rely on the
activerecord associations, and don’t bother even placing a field for the
foreign key in the tables?
It’s not only recommended, it’s required. Whether you are using a
database that enforces foreign key constraints or not, is another
question. But without the foreign key columns in the necessary tables
none of the Rails associations function, at all.
I fail to see how you can develop a proper application without having
these items mapped to the database.
Heh… that’s because you can’t develop a “proper”, or even a
functioning application, that relies on relations between tables,
without foreign key columns. You have no relations without those
columns…
I have been trying to get my schema sorted now for a month and still
don’t know what to do for the best.
http://www.spectrais.com/images/0.png
For some unknown reason I can’t seem to do the following:
customer1 = Client.create(@params[:client])
then customer1.User.create(@params[:user])
Yes. That’s because your second line needs to read:
customer1.user.create(@params[:user])
You need to use the name of the association (ruby is case sensitive) to
reference the related objects, not the name of the parent class of the
related object. If you wanted to create a stand-alone User object, then
you would use “User.create”, just like you did with your stand-alone
Client object in the first statement.
-Brian