Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 13th, 2010, 12:46 PM
jbuckle24 jbuckle24 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Burlington, VT
Posts: 344 jbuckle24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 53 sec
Reputation Power: 13
Facebook
Newbie Modeling/Association Question

Hello --

Be forewarned I am new to RoR, but have spent several days trying to figure this out.

My problem is that I have a database schema where I have a User table & a Task table. The Task table has an assigned_to field and an assigned_by that relates back to the User table.

I figured that the model would be something like

class User< ActiveRecord::Base
has_many :tasks, :foreign_key=>"assigned_by"
has_many :tasks, :foreign_key =>"assigned_to"
end

class Task< ActiveRecord::Base
belongs_to :user
end

I am sure I making a glaring mistake, but just thought I would ask for some help.

thanks in advance.
__________________
------------------------------------------------------------

Reply With Quote
  #2  
Old June 9th, 2010, 01:47 AM
techMonster techMonster is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 107 techMonster User rank is Lance Corporal (50 - 100 Reputation Level)techMonster User rank is Lance Corporal (50 - 100 Reputation Level)techMonster User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 20 h 8 m 58 sec
Reputation Power: 9
Send a message via Yahoo to techMonster Send a message via Google Talk to techMonster
Hi,

First, it would be good if you name your tables as users and tasks (as per the rails convention).

Now coming back to your problem, you could try the following code instead:

Code:
class User< ActiveRecord::Base
  has_many :tasks, :class_name => "Task", :foreign_key=>"assigned_by"
  has_many :todo, :class_name => "Task", :foreign_key =>"assigned_to"
end

class Task< ActiveRecord::Base
  belongs_to :user
end


This way, you can then use user.tasks to see all the tasks assigned by him, and user.todo as the tasks which he needs to do.

I hope I have understood your problem correctly and the solution is what you need.
__________________
@ PresentSoft Technologies Pvt. Ltd. we develop scalable applications using on Ruby on Rails and Android.

My blog @ AnayKamat.com

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Newbie Modeling/Association Question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap