Kris Leech
2005-Oct-09 15:11 UTC
How does child know parent.id in one to many relationship?
If I have a one to many relationship, how do you relate the new child, to the parent? As a practical example say I have a "projects" and "tasks" tables. The tasks table has a project_id field. I generate scaffold for both. I add "belongs_to :projects" to the Project model and "has_many :tasks" to the Tasks table. So I have created the relationship. The next thing to do is add a link on the project''s show view to create a new task. [is this correct?] <%= link_to ''New Task'', :controller => ''task'', :action => ''new'' %> This new task should be linked to the project that is being shown. This is where I trip up! 1.) How does the New method in task controller know what the parent id is? 2.) And secondly how does the Create method get the parent id? 1.) The new task link could include the current projects id <%= link_to ''New Task'', :controller => ''task'', :action => ''new'', :id => @project.id %> 2.) The New method could put the project id in a var which could be written to a hidden field (in the view) which the Create method would get. @project_id = params[:id] Is this correct, or is their a better way of doing this? Maybe storing the id in a session and having both New and Create methods read the session? Is there an automatic, rails way, of doing this? Thanks for any help, K.
Jarkko Laine
2005-Oct-09 16:06 UTC
Re: How does child know parent.id in one to many relationship?
On 9.10.2005, at 18.11, Kris Leech wrote:> I add "belongs_to :projects" to the Project model and > "has_many :tasks" to the Tasks table.Really? Because that''s backwards. Project belonging to a project and a task having many tasks. That''s probably not what you want. You should have Project "has_many :tasks" and Task "belongs_to :project", right? <snip />> 1.) The new task link could include the current projects id > <%= link_to ''New Task'', :controller => ''task'', :action => > ''new'', :id => @project.id %> > > 2.) The New method could put the project id in a var which could be > written to a hidden field (in the view) which the Create method > would get. > @project_id = params[:id]This is fine. In 2) you can do something like this: @task = Task.new @task.project = Project.find(params[:id]) # Or @task.project_id = params[:id] to avoid the db call ...and in the view: <%= hidden_field "task", "project_id" %> (untested code warning) //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Tom Mornini
2005-Oct-09 17:51 UTC
Re: How does child know parent.id in one to many relationship?
On Oct 9, 2005, at 9:06 AM, Jarkko Laine wrote:> On 9.10.2005, at 18.11, Kris Leech wrote: > >> I add "belongs_to :projects" to the Project model and >> "has_many :tasks" to the Tasks table. > > Really? Because that''s backwards. Project belonging to a project > and a task having many tasks. That''s probably not what you want. > You should have Project "has_many :tasks" and Task > "belongs_to :project", right? > > <snip /> > >> 1.) The new task link could include the current projects id >> <%= link_to ''New Task'', :controller => ''task'', :action => >> ''new'', :id => @project.id %> >> >> 2.) The New method could put the project id in a var which could >> be written to a hidden field (in the view) which the Create method >> would get. >> @project_id = params[:id] > > This is fine. In 2) you can do something like this: > > @task = Task.new > @task.project = Project.find(params[:id]) # Or @task.project_id = > params[:id] to avoid the db call > > ...and in the view: > > <%= hidden_field "task", "project_id" %>Why would you store the project_id in a hidden field named task? You could also handle the project ID via the URL: <%= start_form_tag { :action => ''create_seller'', :id => @project } -- -- Tom Mornini
Jarkko Laine
2005-Oct-09 18:14 UTC
Re: How does child know parent.id in one to many relationship?
On 9.10.2005, at 20.51, Tom Mornini wrote:> Why would you store the project_id in a hidden field named task? > > You could also handle the project ID via the URL: > > <%= start_form_tag { :action => ''create_seller'', > :id => @project }You''re right, you don''t need it in this case since the id of the task needs not to be passed (since it doesn''t even have an id yet). //jarkko> -- > -- Tom Mornini > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko Laine
2005-Oct-09 19:52 UTC
Re: How does child know parent.id in one to many relationship?
On 9.10.2005, at 21.14, Jarkko Laine wrote:> > On 9.10.2005, at 20.51, Tom Mornini wrote: > >> Why would you store the project_id in a hidden field named task?Oh, btw, it''s not named task, it''s named task[project_id] so it will be automatically used when the task it created like this: task = Task.new(params[:task]) Like said, it''s less typing to use the method below in this case, but there definitely are situations where hidden fields come in extremely handy. //jarkko>> >> You could also handle the project ID via the URL: >> >> <%= start_form_tag { :action => ''create_seller'', >> :id => @project } >>-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Tom Mornini
2005-Oct-09 20:13 UTC
Re: How does child know parent.id in one to many relationship?
On Oct 9, 2005, at 12:52 PM, Jarkko Laine wrote:> On 9.10.2005, at 21.14, Jarkko Laine wrote: > >> On 9.10.2005, at 20.51, Tom Mornini wrote: >> >>> Why would you store the project_id in a hidden field named task? > > Oh, btw, it''s not named task, it''s named task[project_id] so it > will be automatically used when the task it created like this:Oh, yes, sorry for that. Thanks for the correction. I''m approaching fluency, but I''m in that dangerous period where I''m ''sure'' I understand what the natives are saying. :-) -- -- Tom Mornini
Kris Leech
2005-Oct-11 18:06 UTC
Re: How does child know parent.id in one to many relationship?
>"Really? Because that''s backwards. Project belonging to a project anda task having many tasks. That''s probably >not what you want. You should have Project "has_many :tasks" and Task "belongs_to :project", right?" Sorry yeah, it was a typo on my part, my code is as you describe! @task = Task.new @task.project_id = params[:id] Im sure I tried this already but it did''nt work. I shall have to review my code. But basically this should replace the need to create a seperate var to hold the project id that is assigned to the new task (which will be writtern to a hidden field in the New view)? But... you dont need a hidden field in this case because... Either the Create method is passed the @task var we created in the New action, which has the correct project id already assigned? Or the project id is passed to the Create method as part of the form''s action and the code would be: <%= start_form_tag { :action => ''create'', :id => @project.id } %> I''m I getting there? :)
Jarkko Laine
2005-Oct-12 06:29 UTC
Re: How does child know parent.id in one to many relationship?
On 11.10.2005, at 21.06, Kris Leech wrote:> > >"Really? Because that''s backwards. Project belonging to a project > and a task having many tasks. That''s probably >not what you want. > You should have Project "has_many :tasks" and Task > "belongs_to :project", right?" > > Sorry yeah, it was a typo on my part, my code is as you describe! > > @task = Task.new > @task.project_id = params[:id] > > Im sure I tried this already but it did''nt work. I shall have to > review my code. But basically this should replace the need to > create a seperate var to hold the project id that is assigned to > the new task (which will be writtern to a hidden field in the New > view)? > > But... you dont need a hidden field in this case because... > > Either the Create method is passed the @task var we created in the > New action, which has the correct project id already assigned? > > Or the project id is passed to the Create method as part of the > form''s action and the code would be: > > <%= start_form_tag { :action => ''create'', :id => @project.id } %> > > I''m I getting there? :)No objects are implicitly passed between actions, we''re talking about two separate http requests after all. The two most common ways to pass data in your case are either through the form (as a hidden var) or as part of the url. If you use the latter, you will have the project id at your disposal as params[:id] (in the create method) because you pass it as the id parameter in the form tag. //jarkko> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Kris Leech
2005-Oct-12 09:30 UTC
Re: How does child know parent.id in one to many relationship?
I understand the concept, but am finding the actual example hard to grasp. If I was writing a step by step tutorial (which I will pad out for the wiki), it would go: Assuming tables called Projects and Tasks, and Tasks has an int field called project_id 1. Create scaffold for Project and scaffold for Task 2. Add one to many relationship to models (has_many, belongs_to) 3. Go to the Project''s Show view and add a link to create a new Task, send project id in link. 4. Go to the Task''s New method and add ? 5. Go to the Task''s New view and add ? 6. Go to the Task''s Create method and add? Sorry, can you spell it out for me. I know you have already helped a lot. Many thanks, K. Jarkko Laine wrote:> > On 11.10.2005, at 21.06, Kris Leech wrote: > >> >> >"Really? Because that''s backwards. Project belonging to a project >> and a task having many tasks. That''s probably >not what you want. >> You should have Project "has_many :tasks" and Task "belongs_to >> :project", right?" >> >> Sorry yeah, it was a typo on my part, my code is as you describe! >> >> @task = Task.new >> @task.project_id = params[:id] >> >> Im sure I tried this already but it did''nt work. I shall have to >> review my code. But basically this should replace the need to create >> a seperate var to hold the project id that is assigned to the new >> task (which will be writtern to a hidden field in the New view)? >> >> But... you dont need a hidden field in this case because... >> >> Either the Create method is passed the @task var we created in the >> New action, which has the correct project id already assigned? >> >> Or the project id is passed to the Create method as part of the >> form''s action and the code would be: >> >> <%= start_form_tag { :action => ''create'', :id => @project.id } %> >> >> I''m I getting there? :) > > > No objects are implicitly passed between actions, we''re talking about > two separate http requests after all. The two most common ways to > pass data in your case are either through the form (as a hidden var) > or as part of the url. If you use the latter, you will have the > project id at your disposal as params[:id] (in the create method) > because you pass it as the id parameter in the form tag. > > //jarkko > >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > >------------------------------------------------------------------------ > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Interkonect Services UK Ltd. Boundary House Main Street Hoveringham Nottingham NG147 JR web: www.interkonect.com tel: 0115 9663696 fax: 0115 9663696
Jarkko Laine
2005-Oct-12 10:12 UTC
Re: How does child know parent.id in one to many relationship?
On 12.10.2005, at 12.30, Kris Leech wrote:> I understand the concept, but am finding the actual example hard to > grasp. > If I was writing a step by step tutorial (which I will pad out for > the wiki), it would go: > > Assuming tables called Projects and Tasks, and Tasks has an int > field called project_id > > 1. Create scaffold for Project and scaffold for Task > 2. Add one to many relationship to models (has_many, belongs_to) > 3. Go to the Project''s Show view and add a link to create a new > Task, send project id in link.<%= link_to "Add a task", :controller => "tasks", :action => "new", :id => @project %>> 4. Go to the Task''s New method and add ?Nothing necessarily, @params[:id] will be available to the view as such.> 5. Go to the Task''s New view and add ?<%= start_form_tag { :action => ''create'', :id => @params[:id] } %>> 6. Go to the Task''s Create method and add?project = Project.find(params[:id]) task = project.tasks.create(params[:task]) You can write all this in one line (Project.find(params [:id]).tasks.create(params[:task])) but the above might be a bit easier to grasp for a newbie. Note that all the validation is omitted in the above example. //jarkko> > Sorry, can you spell it out for me. I know you have already helped > a lot. > > Many thanks, K. > > Jarkko Laine wrote: > > >> >> On 11.10.2005, at 21.06, Kris Leech wrote: >> >> >>> >>> >"Really? Because that''s backwards. Project belonging to a >>> project and a task having many tasks. That''s probably >not what >>> you want. You should have Project "has_many :tasks" and Task >>> "belongs_to :project", right?" >>> >>> Sorry yeah, it was a typo on my part, my code is as you describe! >>> >>> @task = Task.new >>> @task.project_id = params[:id] >>> >>> Im sure I tried this already but it did''nt work. I shall have to >>> review my code. But basically this should replace the need to >>> create a seperate var to hold the project id that is assigned to >>> the new task (which will be writtern to a hidden field in the >>> New view)? >>> >>> But... you dont need a hidden field in this case because... >>> >>> Either the Create method is passed the @task var we created in >>> the New action, which has the correct project id already assigned? >>> >>> Or the project id is passed to the Create method as part of the >>> form''s action and the code would be: >>> >>> <%= start_form_tag { :action => ''create'', :id => >>> @project.id } %> >>> >>> I''m I getting there? :) >>> >> >> >> No objects are implicitly passed between actions, we''re talking >> about two separate http requests after all. The two most common >> ways to pass data in your case are either through the form (as a >> hidden var) or as part of the url. If you use the latter, you >> will have the project id at your disposal as params[:id] (in the >> create method) because you pass it as the id parameter in the >> form tag. >> >> //jarkko >> >> >>> >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >> >> -- >> Jarkko Laine >> http://jlaine.net >> http://odesign.fi >> >> --------------------------------------------------------------------- >> --- >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > > -- > Interkonect Services UK Ltd. > Boundary House Main Street > Hoveringham > Nottingham > NG147 JR > > web: www.interkonect.com > tel: 0115 9663696 > fax: 0115 9663696 > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails