Brian Ekmark
2012-Aug-27 18:39 UTC
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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Colin Law
2012-Aug-28 20:09 UTC
Re: New to RoR: Need some assistance displaying data from multiple tables.
On 27 August 2012 19:39, Brian Ekmark <brian.ekmark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Thomas Chen
2012-Aug-28 20:48 UTC
Re: New to RoR: Need some assistance displaying data from multiple tables.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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
2012-Sep-05 16:19 UTC
Re: New to RoR: Need some assistance displaying data from multiple tables.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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
2012-Sep-05 16:27 UTC
Re: New to RoR: Need some assistance displaying data from multiple tables.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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
2012-Sep-06 05:52 UTC
Re: New to RoR: Need some assistance displaying data from multiple tables.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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
2012-Sep-07 14:29 UTC
Re: New to RoR: Need some assistance displaying data from multiple tables.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.