tag:blogger.com,1999:blog-6793817269158736634.post9194752362557746037..comments2023-07-11T06:44:05.157-07:00Comments on Code It Like You Stole It: Mo models, mo problemsTylerhttp://www.blogger.com/profile/02372203985854517525noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-6793817269158736634.post-43825892548863398232011-05-03T10:16:12.148-07:002011-05-03T10:16:12.148-07:00Technically has_one and has_many are the same thin...Technically has_one and has_many are the same thing, but has_one is just limited to 1 row.<br /><br />In this case it will make interfaces to this code significantly easier to develop.Elijah Millerhttps://www.blogger.com/profile/11416221333112662701noreply@blogger.comtag:blogger.com,1999:blog-6793817269158736634.post-28566048500401306722011-05-03T09:27:43.097-07:002011-05-03T09:27:43.097-07:00I would have to understand the problem better but ...<i>I would have to understand the problem better but it seems quite likely you may prefer the ease of a statistic model that has many columns for statistics instead of many statistics.</i><br /><br />Yeah, this is how I coded it to begin with. The statistic model has 6 columns. It's not a matter of having separate models for Strength, Intelligence, etc. But you can still use has_one in this case, correct? The number of columns isn't referenced by has_one or has_many, it's the instances of the model? So if I had a statistic model with one column for each statistic, I'd need to use has_many, but if I have one statistic model with 6 columns, I can use has_one?<br /><br /><i>Another solution is to simply disable the forced login. Just comment out the before_filter.</i> <br /><br />I didn't think of doing this as a workaround and it sounds like the right solution. I've got a tendency to make small problems seem larger than they are! See also: foreign keys.<br /><br />Thanks again for the advice. Sucks I'm stuck at work for another 4 hours. MUST CODE!Tylerhttps://www.blogger.com/profile/02372203985854517525noreply@blogger.comtag:blogger.com,1999:blog-6793817269158736634.post-41063892291287096572011-05-03T09:09:48.777-07:002011-05-03T09:09:48.777-07:00Wow, lots of questions here, let me break these do...Wow, lots of questions here, let me break these down.<br /><br />RE each character will have multiple statistics.<br />I think you will find there may be a simpler way to deal with this in code. I would have to understand the problem better but it seems quite likely you may prefer the ease of a statistic model that has many columns for statistics instead of many statistics. Both can work, one is more flexible, and one is easier to code. I would tend toward one statistic model per character.<br /><br />RE forced login<br />This may be a good opportunity to make a user signup page.<br /><br />Also, you can create a user from the console with ease. User.create(:email => 'joe@sixpack.com', ...). Your authentication system will have other options that are required I'm sure but that's the gist. If you made a signup/register page the controller undoubtedly makes this call and you can probably look at it to see how it works.<br /><br />Another solution is to simply disable the forced login. Just comment out the before_filter.<br /><br />RE multiple stats instances<br />This is completely up to you, but as I said before I would tend towards a has_one statistic. It's easier to develop and use, but less flexible than has_many statistics.<br /><br />RE sqlite3 foreign keys<br />It handles it roughly the same as all SQL databases. Chances are you're not using foreign key constraints (not recommended here), so in fact it is identical to all other databases in this respect.<br /><br />The foreign key is the "key from one piece of data that points you at another(foreign) one." In this case, the foreign key is character_id on statistics. Without this key, the data would not be associated.<br /><br />When creating a model through an association (@character.statistics.create) this foreign key should be set for you already. Imagine you called Statistic.create, the code it would have no idea which character to associate it to, but by calling it through the association you make it clear to ActiveRecord that this foreign key should be set.Elijah Millerhttps://www.blogger.com/profile/11416221333112662701noreply@blogger.com