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/-/GoXlLKpnSTAJ.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Colin Law at Aug 28, 2012 at 8:09 pm

    On 27 August 2012 19:39, 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:
    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

    --
    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.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Thomas Chen at Sep 1, 2012 at 10:01 am
    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.
  • Brian Ekmark at Sep 5, 2012 at 4:19 pm
    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 | %>
    <div>
    <h3><%= mytasks.task_id %></h3>
    </div>
    <% 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
    On Tuesday, August 28, 2012 3:48:13 PM UTC-5, Thomas Chen wrote:

    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/-/_Hf91BPrHeIJ.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brian Ekmark at Sep 5, 2012 at 4:27 pm
    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
    On Monday, August 27, 2012 1:39:04 PM UTC-5, 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/-/oMNPb926RBcJ.
    For more options, visit https://groups.google.com/groups/opt_out.
  • uma mahesh varma Seeram at Sep 6, 2012 at 5:52 am
    Hi,

    What will be the query that runs for above association ?

    Thank You,
    Uma Mahesh.

    On Wednesday, September 5, 2012 9:57:37 PM UTC+5:30, Brian Ekmark wrote:

    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
    On Monday, August 27, 2012 1:39:04 PM UTC-5, 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/-/ERvs7_3xKz4J.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brian Ekmark at Sep 7, 2012 at 2:30 pm
    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
    wrote:
    Hi,

    What will be the query that runs for above association ?

    Thank You,
    Uma Mahesh.

    On Wednesday, September 5, 2012 9:57:37 PM UTC+5:30, Brian Ekmark wrote:

    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
    On Monday, August 27, 2012 1:39:04 PM UTC-5, 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/-/f7snwAxKRAcJ.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedAug 28, '12 at 7:27p
activeSep 7, '12 at 2:30p
posts7
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase