Ruby on Rails Tuesday, August 28, 2012

It looks like you have a pretty classic has_many through kind of relationship. I don't know the details of your model, but I'd assume you have the following 3 models:

User:
 id

TaskRelationship
 user_id
 task_id

Task
 id

What you want to do, then is to put in your user model the following line:
has_many task_relationships
has_many :tasks, :through => :task_relationships

And the following into your taskrelationship model
belongs_to :user
belongs_to :task

Doing this will allow rails to automatically join your models as necessary allowing you to do stuff like:
user1.tasks
when you want to pull out all the tasks that belong to user1

On Monday, August 27, 2012 11:39:04 AM UTC-7, Brian Ekmark wrote:
Greetings --
 
I am very new to RoR and I am reaching out for some assistance. I am creating a task management web app to practice and I'm not sure if I am running into more of a syntax issue or a design issue. The problem I am having is that I am trying to reference a task for a specific user. I have two seperate tables for both the users and the tasks, but on the task create method I also write to a third table which ties the user to the task via their own id. I have been able to keep the creation and the destory in sync, but the problem is that I'm not sure how to display the contents of the task record from the third table which ties the user to the task. Below are some specifics if anyone could help me understand this better I would greatly appreciate it.
 
I am trying to link these all together from a home controller. I can find the assigned task_id from the reference table, but I don't know how to extract the contents of the record:
 
class HomeController < ApplicationController
  def index
    @mytasks = TaskOwner.where( :user_id => session[:user_id] )
  end
end
Thank you in advance,
 
Brian
 
 
 

--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/4-7g9x9yqasJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment