David Balatero
2006-May-30 23:23 UTC
[Rails] Having two columns in a model point to another table
Hey, I''m kind of new to Rails, and would appreciate it if someone could point me in the right direction on this. I have a "users" table in my application, and I wanted to set up a "debts" table, to store debts between users. My current table structure for "debts" is CREATE TABLE debts ( id int not null auto_increment, owed_user_id int not null, # User that is owed the money owee_user_id int not null, # User that owes money to the owed_user reason text not null, # Reason for debt amount decimal(10,2) not null, primary key(id) ); In this case, both owed_user_id and owee_user_id point to a different user in the Users table. How can I represent this in my model class for Users and Debts using Rails associations? None of the associations seemed to fit for me. Maybe I''m thinking of the table structure in the wrong manner, and there''s a better way to do that given the constraints of Rails associations. Any help would be appreciated! Thanks! David Balatero -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/f0eca62c/attachment.html
Josh Susser
2006-May-30 23:41 UTC
[Rails] Re: Having two columns in a model point to another table
David Balatero wrote:> Hey, I''m kind of new to Rails, and would appreciate it if someone could > point me in the right direction on this. > > I have a "users" table in my application, and I wanted to set up a > "debts" > table, to store debts between users. > > My current table structure for "debts" is > > CREATE TABLE debts ( > id int not null auto_increment, > owed_user_id int not null, # User that is owed the money > owee_user_id int not null, # User that owes money to the owed_user > reason text not null, # Reason for debt > amount decimal(10,2) not null, > primary key(id) > );This is a job for a self-referential has_many :through association. You can read an example of that here: http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through You''d set up your users where I have nodes in my example, and debts would be edges. Creditors and debtors would go in the place of sources and sinks. Have fun! -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Maybe Matching Threads
- Scheduled workers only run once unless you call self.delete inside the worker
- Cannot start workers in production mode
- Scheduled worker dies after about 30-45 runs
- http://www.linuxquestions.org/questions/showthread.php?p=4266798#post4266798
- Turning off activerecord transactions