Ruby on Rails Monday, March 28, 2011

It is indeed redundant to have both #set_primary_key and the :primary_key option w/in your #has_many (or #has_one) calls.

You can safely ignore that aspect of my prior posts :). The real code I yanked my examples from actually has a table with a primary key named "ID" (so set_primary_key "ID") but has another column "UserID" (that is also unique and *could* have been the primary key instead; crazy old db) that for all relationships is treated as if it were the primary key.

So, in reality my model is more like this:

class User < ActiveRecord::Base
  set_table_name "tblUser"
  set_primary_key "ID"
  has_many :comments, :primary_key => "UserID",
    :foreign_key => "PosterID"
end

That's what I get for not simply copy/pasting (then nuking irrelevant details) and hand-coding my example from memory.

Anyhow, your other observation is also dead on. Creating an association on a model doesn't require you to create the "inverse" association on the other model.

> P.S. the reason i am torturing Rails like that is that it also has a
> convention that it should obey the human.
> Seriously, i just want a nice and clear structure of my database, which
> would be editable by hand (it is not going to be big), and to use Rails
> just for a simple interface.

While I always follow rails conventions when possible (and recommend others do too), I also like to "torture" frameworks to see how flexible they are. After all, there are always occasions where it becomes necessary, such as in my instance where I have to work off of a legacy database that is still concurrently being accessed by legacy software. On the side, however, I've instantiated a dozen smaller "toy" or "utility" rails apps where I've followed the conventions and idioms religiously to great effect. Love 'em.


--
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 rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment