Adam Teale
2008-Jul-03 06:56 UTC
q: how to list one model in a different model''s view form
Hi Guys, i have a the following models: job has_many :job_divisions has_many :divisions division belongs_to :job has_many :job_divisions job_division belongs_to :job belongs_to :division When I do a Job.new I would like to have a list of all the divisions, and be able to select them with checkboxes. Once the Job.create is run a job_division.create is run for each of the selected divisions in the above list. Something like that. I can''t work out how to get the list of Divisions to appear in the View for the Job.new, and then how to get it to save all the info. Any ideas? Any pointers would be great Cheers Adam -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
\"Wolas!\"
2008-Jul-03 07:27 UTC
Re: q: how to list one model in a different model''s view form
job has_and_belongs_to_many :divisions division has_and_belongs_to_many: jobs divisions_jobs (its a many to many join table, rails conventions make it alphabetical order and plurars) has no model but in the table should be job_id:integer division_id:integer then you can do in the view <%= check_box_tag "job[division_ids][]", division.id, @job.divisions.include?(division) %> <%= division.name %> Remember that in HTML, when checkboxes are not checked, they DONT appear in params at all (as opposed to appeaaring as :name => false) here is a source that might help: http://railscasts.com/episodes/17 its a bit old (rails 1.2) but still aplies hope this helps On Jul 3, 7:56 am, Adam Teale <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi Guys, > > i have a the following models: > > job > has_many :job_divisions > has_many :divisions > > division > belongs_to :job > has_many :job_divisions > > job_division > belongs_to :job > belongs_to :division > > When I do a Job.new I would like to have a list of all the divisions, > and be able to select them with checkboxes. > Once the Job.create is run a job_division.create is run for each of the > selected divisions in the above list. > Something like that. > > I can''t work out how to get the list of Divisions to appear in the View > for the Job.new, and then how to get it to save all the info. > > Any ideas? Any pointers would be great > > Cheers > > Adam > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Adam Teale
2008-Jul-03 09:49 UTC
Re: q: how to list one model in a different model''s view for
Wolas thankyou very much! I got solution over at railforum.com as well: http://railsforum.com/viewtopic.php?pid=67294#p67294 Thankyou very much! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Adam Teale
2008-Jul-03 18:28 UTC
Re: q: how to list one model in a different model''s view for
I have another question, I would like to use the same setup but this time with a text_field in the form. The additional field is for a column in the divisions_jobs called ''shotamount'' [code] <% @job.divisions.each do |d| %> <p><%= text_field_tag d.shotamount %> <%= d.name %><br/> <% end %> [/code] The form loads fine, I can type in numbers to the text_fields, and then hit submit. However the divisions_jobs.shotamount is not updating in table. Any ideas? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
\"Wolas!\"
2008-Jul-03 19:28 UTC
Re: q: how to list one model in a different model''s view for
> <p><%= text_field_tag d.shotamount %> <%= d.name %><br/>1) You have the shotamount pointing to the divisions table, not the joined table. (d.shotamount? then the model division has a column shotamount or a method shotamount?) 2) The text_field_tag gives you a plain text field. the controller will not use the value at all to do mass assignment (what it does to update_attributes or Model.new(:foo => ''bar'' ). 3) if you look at the api: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001698 you have put the wrong arguments in the wrong places. Ruby will let you do this (its not like java, and rails will do even more auto-magik (accept integers as strings and vice versa) ) so always always read the api of a function if you are unsure. 4) in HABTM you cant have rich associations. ie: no more attributes than 2 (maybe more) ids and thats it. if you need rich associations, you ill have to use has_many :through. there are plenty of blogs about i recommend Josh suser (i think its spelt like this) hope it helps On Jul 3, 7:28 pm, Adam Teale <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have another question, I would like to use the same setup but this > time with a text_field in the form. > > The additional field is for a column in the divisions_jobs called > ''shotamount'' > > [code] > <% @job.divisions.each do |d| %> > <p><%= text_field_tag d.shotamount %> <%= d.name %><br/> > <% end %> > [/code] > > The form loads fine, I can type in numbers to the text_fields, and then > hit submit. > > However the divisions_jobs.shotamount is not updating in table. > > Any ideas? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Adam Teale
2008-Jul-04 08:04 UTC
Re: q: how to list one model in a different model''s view for
Hi Wolas thanks for continuing to help me out ! I am now trying to do this with "has_many :through" as you suggested but have been losing hair trying to work this out. I don''t really get it, and the frustration of not understanding is killing me! Would you be able to have a look again? Thanks if you can! Adam #JOB MODEL class Job < ActiveRecord::Base has_many :job_divisions has_many :divisions, :through => :job_divisions def self.options_for_select self.find(:all).collect{|job| [job.job_title]} end def division_ids(divids=[]) divids.each do |divid| job_divisions.build(:division_id => divid) end end end #DIVISION MODEL class Division < ActiveRecord::Base has_many :job_divisions has_many :jobs, :through => :job_divisions end #JOB_DIVISION MODEL class JobDivision < ActiveRecord::Base belongs_to :job belongs_to :division end #JOB VIEW <h1>New job</h1> <% form_for(@job) do |f| %> <p> <b>Job Title</b><br /> <%= f.text_field :job_title %> </p> <b>Division</b><br /> <% Division.find(:all).each do |d| %> <%= check_box_tag "job[division_ids][]", d.id %> <%= d.name %> <% end %> <p> <%= f.submit "Create" %> </p> <% end %> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
\"Wolas!\"
2008-Jul-04 10:03 UTC
Re: q: how to list one model in a different model''s view for
Ahhh, we have switched methods! the "job[division_ids][]" business works for HABTM. so: what you want to do is attach a division to a job via a checkbox, so the presence of that check box means the division will be added. ok. <%= check_box_tag "divisions[#{division.name}]", true, job.divisions.include?(division) %> this will create a params in the form of: {"divisions" => {''finance'' => true, ''it'' => ''true''} } this is not understood by rails by default. so we have to work a bit in the controller. (most important thing is to remember that if the check box is unchecked it wil NOT give ''name'' => false. it will just not appear at all) def update job = Job.find(params[:id]) # here is the beauty of ruby...whatever gets returned by the if # block gets assigned to jobs-new-divisions jobs_new_divisions = if params[:divisions] # we only want the name because presence means it was checked params[:divisions].map{|key, value| key } else # just in case we have everything unchecked [] end if job.update_attributes(params[:job]) Job.divisions = jobs_new_divisions Job.save flash[:notice] = "Roles for #{player.name} succesfully updated" redirect_to :action => ''index'' else # bla bla bla end end hope it helps On Jul 4, 9:04 am, Adam Teale <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi Wolas thanks for continuing to help me out ! > > I am now trying to do this with "has_many :through" as you suggested but > have been losing hair trying to work this out. I don''t really get it, > and the frustration of not understanding is killing me! > > Would you be able to have a look again? > Thanks if you can! > Adam > > #JOB MODEL > class Job < ActiveRecord::Base > has_many :job_divisions > has_many :divisions, :through => :job_divisions > > def self.options_for_select > self.find(:all).collect{|job| [job.job_title]} > end > > def division_ids(divids=[]) > divids.each do |divid| > job_divisions.build(:division_id => divid) > end > end > end > > #DIVISION MODEL > class Division < ActiveRecord::Base > has_many :job_divisions > has_many :jobs, :through => :job_divisions > end > > #JOB_DIVISION MODEL > class JobDivision < ActiveRecord::Base > belongs_to :job > belongs_to :division > end > > #JOB VIEW > <h1>New job</h1> > > <% form_for(@job) do |f| %> > <p> > <b>Job Title</b><br /> > <%= f.text_field :job_title %> > </p> > > <b>Division</b><br /> > > <% Division.find(:all).each do |d| %> > <%= check_box_tag "job[division_ids][]", d.id %> <%= d.name %> > <% end %> > > <p> > <%= f.submit "Create" %> > </p> > <% end %> > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
\"Wolas!\"
2008-Jul-04 10:07 UTC
Re: q: how to list one model in a different model''s view for
even better (refactoring...) def update job = Job.find(params[:id]) if job.update_attributes(params[:job]) # here is the beauty of ruby...whatever gets returned by the if # block gets assigned to jobs-new-divisions jobs.divisions = if params[:divisions] # we only want the name because presence means it was checked params[:divisions].map{|key, value| key } else # just in case we have everything unchecked [] end Job.save! #bang because we should be certain that everything is ok. flash[:notice] = "#{job.name} succesfully updated" redirect_to :action => ''index'' else # bla bla bla end end On Jul 4, 11:03 am, "\"Wolas!\"" <jcpen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ahhh, we have switched methods! the "job[division_ids][]" business > works for HABTM. so: > > what you want to do is attach a division to a job via a checkbox, so > the presence of that check box means the division will be added. ok. > > <%= check_box_tag "divisions[#{division.name}]", true, > job.divisions.include?(division) %> > > this will create a params in the form of: > > {"divisions" => {''finance'' => true, ''it'' => ''true''} } > > this is not understood by rails by default. so we have to work a bit > in the controller. (most important thing is to remember that if the > check box is unchecked it wil NOT give ''name'' => false. it will just > not appear at all) > > def update > job = Job.find(params[:id]) > > # here is the beauty of ruby...whatever gets returned by the if > # block gets assigned to jobs-new-divisions > jobs_new_divisions = if params[:divisions] > # we only want the name because presence means it was checked > params[:divisions].map{|key, value| key } > else > # just in case we have everything unchecked > [] > end > > if job.update_attributes(params[:job]) > Job.divisions = jobs_new_divisions > Job.save > flash[:notice] = "Roles for #{player.name} succesfully updated" > redirect_to :action => ''index'' > else > # bla bla bla > end > end > > hope it helps > > On Jul 4, 9:04 am, Adam Teale <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Hi Wolas thanks for continuing to help me out ! > > > I am now trying to do this with "has_many :through" as you suggested but > > have been losing hair trying to work this out. I don''t really get it, > > and the frustration of not understanding is killing me! > > > Would you be able to have a look again? > > Thanks if you can! > > Adam > > > #JOB MODEL > > class Job < ActiveRecord::Base > > has_many :job_divisions > > has_many :divisions, :through => :job_divisions > > > def self.options_for_select > > self.find(:all).collect{|job| [job.job_title]} > > end > > > def division_ids(divids=[]) > > divids.each do |divid| > > job_divisions.build(:division_id => divid) > > end > > end > > end > > > #DIVISION MODEL > > class Division < ActiveRecord::Base > > has_many :job_divisions > > has_many :jobs, :through => :job_divisions > > end > > > #JOB_DIVISION MODEL > > class JobDivision < ActiveRecord::Base > > belongs_to :job > > belongs_to :division > > end > > > #JOB VIEW > > <h1>New job</h1> > > > <% form_for(@job) do |f| %> > > <p> > > <b>Job Title</b><br /> > > <%= f.text_field :job_title %> > > </p> > > > <b>Division</b><br /> > > > <% Division.find(:all).each do |d| %> > > <%= check_box_tag "job[division_ids][]", d.id %> <%= d.name %> > > <% end %> > > > <p> > > <%= f.submit "Create" %> > > </p> > > <% end %> > > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Adam Teale
2008-Jul-04 11:18 UTC
Re: q: how to list one model in a different model''s view for
Hi Wolas! Thankyou so much for your efforts to explain all this. I have used your code but rails seems to not know about the variable ''division'' :) If you have a paypal account I will happily drop some money in it! I am so brain dead after looking at this all day! Thanks! Adam ################################# NameError in Jobs#new Showing jobs/new.html.erb where line #16 raised: undefined local variable or method `division'' for #<ActionView::Base:0x2624f24> Extracted source (around line #16): 13: <b>Division</b><br /> 14: 15: <% Division.find(:all).each do |d| %> 16: <%= check_box_tag "divisions[#{division.name}]", true, job.divisions.include?(division) %> 17: <% end %> 18: 19: <p> RAILS_ROOT: /Users/ateale/rails/frameworx Application Trace | Framework Trace | Full Trace app/views/jobs/new.html.erb:16:in `_run_erb_47app47views47jobs47new46html46erb'' app/views/jobs/new.html.erb:15:in `each'' app/views/jobs/new.html.erb:15:in `_run_erb_47app47views47jobs47new46html46erb'' app/views/jobs/new.html.erb:7:in `_run_erb_47app47views47jobs47new46html46erb'' app/controllers/jobs_controller.rb:40:in `new'' Request Parameters: None Show session dump Response Headers: {"cookie"=>[], "Content-Type"=>"text/html", "Cache-Control"=>"no-cache"} ################################# -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
martin curtis
2008-Jul-04 11:28 UTC
Re: q: how to list one model in a different model''s view for
hi adam, i think your problem is that you''ve declared ''d'' as the local variable you''re using with the enumeration (when you say, ''each do |d|''. try changing |d| to |division| and see if that helps. hth phantom Quoting Adam Teale <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Hi Wolas! > > Thankyou so much for your efforts to explain all this. > > I have used your code but rails seems to not know about the variable > ''division'' > > :) If you have a paypal account I will happily drop some money in it! > > I am so brain dead after looking at this all day! > > Thanks! > > Adam > > > > ################################# > NameError in Jobs#new > > Showing jobs/new.html.erb where line #16 raised: > > undefined local variable or method `division'' for > #<ActionView::Base:0x2624f24> > Extracted source (around line #16): > > 13: <b>Division</b><br /> > 14: > 15: <% Division.find(:all).each do |d| %> > 16: <%= check_box_tag "divisions[#{division.name}]", true, > job.divisions.include?(division) %> > 17: <% end %> > 18: > 19: <p> > RAILS_ROOT: /Users/ateale/rails/frameworx > > Application Trace | Framework Trace | Full Trace > app/views/jobs/new.html.erb:16:in > `_run_erb_47app47views47jobs47new46html46erb'' > app/views/jobs/new.html.erb:15:in `each'' > app/views/jobs/new.html.erb:15:in > `_run_erb_47app47views47jobs47new46html46erb'' > app/views/jobs/new.html.erb:7:in > `_run_erb_47app47views47jobs47new46html46erb'' > app/controllers/jobs_controller.rb:40:in `new'' > Request > > Parameters: > > None > Show session dump > > Response > > Headers: > > {"cookie"=>[], > "Content-Type"=>"text/html", > "Cache-Control"=>"no-cache"} > ################################# > -- > Posted via http://www.ruby-forum.com/. > > > >-- ------------------------------------------------- Visit Pipex Business: The homepage for UK Small Businesses Go to http://www.pipex.co.uk/business-services --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
\"Wolas!\"
2008-Jul-04 12:39 UTC
Re: q: how to list one model in a different model''s view for
Exactly. sorry, i did a quick copy and paste and forgot about that For the paypal, please, keep it and invite one of your pals to a beer, celebrating the weekend. glad to help j On Jul 4, 12:28 pm, martin curtis <mar...-mzrwy28g8S1JyDwdID6HP9BPR1lH4CV8@public.gmane.org> wrote:> hi adam, > > i think your problem is that you''ve declared ''d'' as the local variable you''re using with the enumeration (when you > say, ''each do |d|''. > > try changing |d| to |division| and see if that helps. > > hth > > phantom > > Quoting Adam Teale <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: > > > > > > > Hi Wolas! > > > Thankyou so much for your efforts to explain all this. > > > I have used your code but rails seems to not know about the variable > > ''division'' > > > :) If you have a paypal account I will happily drop some money in it! > > > I am so brain dead after looking at this all day! > > > Thanks! > > > Adam > > > ################################# > > NameError in Jobs#new > > > Showing jobs/new.html.erb where line #16 raised: > > > undefined local variable or method `division'' for > > #<ActionView::Base:0x2624f24> > > Extracted source (around line #16): > > > 13: <b>Division</b><br /> > > 14: > > 15: <% Division.find(:all).each do |d| %> > > 16: <%= check_box_tag "divisions[#{division.name}]", true, > > job.divisions.include?(division) %> > > 17: <% end %> > > 18: > > 19: <p> > > RAILS_ROOT: /Users/ateale/rails/frameworx > > > Application Trace | Framework Trace | Full Trace > > app/views/jobs/new.html.erb:16:in > > `_run_erb_47app47views47jobs47new46html46erb'' > > app/views/jobs/new.html.erb:15:in `each'' > > app/views/jobs/new.html.erb:15:in > > `_run_erb_47app47views47jobs47new46html46erb'' > > app/views/jobs/new.html.erb:7:in > > `_run_erb_47app47views47jobs47new46html46erb'' > > app/controllers/jobs_controller.rb:40:in `new'' > > Request > > > Parameters: > > > None > > Show session dump > > > Response > > > Headers: > > > {"cookie"=>[], > > "Content-Type"=>"text/html", > > "Cache-Control"=>"no-cache"} > > ################################# > > -- > > Posted viahttp://www.ruby-forum.com/. > > -- > > ------------------------------------------------- > Visit Pipex Business: The homepage for UK Small Businesses > > Go tohttp://www.pipex.co.uk/business-services--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Adam Teale
2008-Jul-05 01:25 UTC
Re: q: how to list one model in a different model''s view for
Wolas & Martin thanks again! The Job.New form now runs without error!!! Cool!! But, the job_division model (join?) table does not get the job''s divisions saved to it. Any ideas? Thanks! Adam -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---