New to RoR: Need some assistance displaying data from multiple tables

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

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 27 August 2012 19:39, Brian E. [email protected] wrote:

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:

Since you have not mentioned the associations between the models (user
has_many tasks for example) I guess that you are not familiar with
such concepts. Have a look at the Rails Guide on Associations (and
all the other guides for that matter). In fact I strongly suggest
that you work right through a tutorial such as railstutorial.org,
which is free to use online, which will introduce you to the basic
concepts of rails. Make sure that the tutorial is for at least rails
3 and that you use the correct version for that tutorial. By the end
of that you will likely be able to answer the questions yourself.

Colin

Thanks, Thomas. I didn’t know about the additional switches on the
relationships. Now that I have the relationship formed I still don’t
understand what I need to use to extract it from the joined tables. Here
is
a better idea of what I have:

User Model
has_many :task, :through => :task_owner
has_many :task_owner

Task Owner Model
belongs_to :task
belongs_to :user

Task Model:
has_many :task_owner, dependent: :destroy

So that should take care of my relationships in the models. So I created
a
Home controller with the following code:

def index
@mytasks = TaskOwner.where( :user_id => session[:user_id] )
end

And on the index page I loop the results:

<% @mytasks.each do | mytasks | %>

<%= mytasks.task_id %>

<% end %>

The output will show me the task_id of the tasks belonging to the user
that
is logged in, but if I change it to something like mytasks.title I get
the
following error:

undefined method `title’ for #TaskOwner:0xb28011c

So I guess I don’t understand how display the contents still.

Thanks,

Brian

Hi,

What will be the query that runs for above association ?

Thank You,
Uma Mahesh.

Nevermind … I figured it out. I was stopping too short into the
methods.

Instead of mytasks.title I should have been using mytasks.task.title

This dug down in the the linked/joined table just fine.

-Brian

Hello Uma,

I am calling this from my Home Controller, but upon a successful login I
store the user_id as a session, but here is the code that I am using and
if
I understand it correctly RoR handles the rest:

class HomeController < ApplicationController
def index
@mytasks = TaskOwner.where( :user_id => session[:user_id] )
end
end

On Thursday, September 6, 2012 12:52:11 AM UTC-5, uma mahesh varma
Seeram

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs