On 26 March 2012 11:19, bingo bob <[email protected]> wrote: > Colin Law wrote in post #1052973: >> On 23 March 2012 16:48, bingo bob <[email protected]> wrote: >>> >>>>> Freddy (2, male) >>>> >>>> What if one of the Freddys is female? Maybe not likely with this name >>>> but there are names that can be both. >>> >>> True - in my system the user simply designates a name as either M or F, >>> gender is an attribute of name...quite possible to have the same name as >>> both M and F and rightly so, actually current schema attached in case it >>> helps. >> >> I think you would be better to have a join table, user_names maybe. >> Then have >> User >> has_many :user_names >> has_many :names, :through => :user_names >> >> UserName >> belongs_to :user >> belongs_to :name >> >> Name >> has_many :user_names >> has_many :users, :through => :user_names >> >> Have given and gender in names and the position in user_names >> >> I think that might help a lot. For example to find how many users >> have a name you can then just use @name.users.count. >> >> Colin > > Thanks for taking the time Colin, that's thought provoking in several > ways. > > 1) You're right the ability to do simply @name.users.count would be > excellent. > > 2) I've got an existing schema in place and am wondering if I can > migrate to this new schema (perhaps with some additional migrations and > rake tasks) or maybe that's a very tricky task.
Never be afraid to refactor if you find a better way to do something. You should always have good automated test coverage so you can be reasonably confident that you have not broken anything. I hope you are using a version control system (such as git) so that if you decide it is all a disaster then you can easily go back to where you were. > > 3) Linked to two, think carefully about the data model before starting > out. Don't spend /too/ much time thinking about it beforehand, you can always refactor it later. > > I don't think what I've got is wrong it's just that @name.users.count > and the like seems like the most elegant way to handle the queries I > need to be doing. In that case what you have got is wrong in the sense that something else would be better. > > The idea of a join table scares me a bit but I get where you're coming > from. If you are going to get anywhere with Rails then you will need to know how to use join tables so take this opportunity to learn. Colin -- 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 [email protected]. 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.

