Adrie Donker
2007-Dec-16 20:39 UTC
Updating HABTM records is easy, but why isn’t one_to_many ?
Updating HABTM records is easy, but why isnt one_to_many (has_many and belongs_to)? With the collection_select it is easy to update HABTM records, everthing is taken care of, besides declar-ing the variable for all records in de related tabel. But updating more than one record from a belongs_to table seems to need a lot of more coding. Or am I missing something? Models: class Event < ActiveRecord::Base has_many :evtimes end class Evtime < ActiveRecord::Base belongs_to :event end In EventsController.rb def edit @event = Event.find(params[:id]) @evtimes = Evtime.find(:all, :conditions => ["event_id = ?", @event.id]) end def update @event = Event.find(params[:id]) @evtimes = Evtime.find(params[:evtimes]) @event << @evtimes if @event.update_attributes(params[:event]) flash[:notice] = ''Event was successfully updated.'' redirect_to :action => ''show'', :id => @event else render :action => ''edit'' end end And in _form.rhtml <%= error_messages_for ''event'' %> <!--[form:event]--> <p><label for="event_name">Name</label><br/> <%= text_field ''event'', ''name'' %></p> <p><label for="event_city">City</label><br/> <%= text_field ''event'', ''city'' %></p> <p><label for="event_description">Description</label><br/> <%= text_field ''event'', ''description'' %></p> <br/> <% for evtime in @evtimes do %> Starting :<%= time_select ''evtime'', ''starting'' %><br/> Ending :<%= time_select ''evtime'', ''ending'' %><br/> Action: <%= text_field ''evtime'', ''movement''%><br/> Notes:<br/> <%= text_area ''evtime'', ''notes'' %><br/> <% end %> <!--[eoform:event]--> Ive tried a lot of combinations, but nothing went the right way. Please help, Im starting to become a Rubyist, but this kind of problems keep diappointing me. Thanks, Adrie -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2007-Dec-16 22:40 UTC
Re: Updating HABTM records is easy, but why isn’t one_to_many ?
What is evtime? You didn''t explain this at all in your code. Is it event time? Why is it being stored in a separate table? On Dec 17, 2007 7:09 AM, Adrie Donker <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Updating HABTM records is easy, but why isn''t one_to_many (has_many and > belongs_to)? > > With the collection_select it is easy to update HABTM records, everthing > is taken care of, besides declar-ing the variable for all records in de > related tabel. But updating more than one record from a belongs_to table > seems to need a lot of more coding. Or am I missing something? > > Models: > class Event < ActiveRecord::Base > has_many :evtimes > end > > class Evtime < ActiveRecord::Base > belongs_to :event > end > > In EventsController.rb > def edit > @event = Event.find(params[:id]) > @evtimes = Evtime.find(:all, :conditions => ["event_id = ?", > @event.id]) > end > > def update > @event = Event.find(params[:id]) > @evtimes = Evtime.find(params[:evtimes]) > @event << @evtimes > if @event.update_attributes(params[:event]) > flash[:notice] = ''Event was successfully updated.'' > redirect_to :action => ''show'', :id => @event > else > render :action => ''edit'' > end > end > > And in _form.rhtml > <%= error_messages_for ''event'' %> > > <!--[form:event]--> > <p><label for="event_name">Name</label><br/> > <%= text_field ''event'', ''name'' %></p> > > <p><label for="event_city">City</label><br/> > <%= text_field ''event'', ''city'' %></p> > > <p><label for="event_description">Description</label><br/> > <%= text_field ''event'', ''description'' %></p> > > <br/> > <% for evtime in @evtimes do %> > Starting :<%= time_select ''evtime'', ''starting'' %><br/> > Ending :<%= time_select ''evtime'', ''ending'' %><br/> > Action: <%= text_field ''evtime'', ''movement''%><br/> > Notes:<br/> <%= text_area ''evtime'', ''notes'' %><br/> > <% end %> > <!--[eoform:event]--> > > I''ve tried a lot of combinations, but nothing went the right way. > Please help, I''m starting to become a Rubyist, but this kind of problems > keep diappointing me. > > Thanks, > Adrie > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Adrie Donker
2007-Dec-18 11:51 UTC
Re: Updating HABTM records is easy, but why isn’t one_to_many ?
Ryan Bigg wrote:> What is evtime? You didn''t explain this at all in your code. > > Is it event time? Why is it being stored in a separate table? > > On Dec 17, 2007 7:09 AM, Adrie Donker <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > >> class Event < ActiveRecord::Base >> @evtimes = Evtime.find(:all, :conditions => ["event_id = ?", >> else >> >> Action: <%= text_field ''evtime'', ''movement''%><br/> >> --Ryan, thanks for your reply, Evtime is short for eventtimes. I want to be able to update eventtimes(Evtime records) on the same page as the related Event (Event record). My problem is (was?) that i can''t get the event.evtimes updateable. But i haven''t stop searching and found this. I did not yet try it. <% @event.evtimes.each do |@evtime| %> Starting :<%= time_select "evtime[]", "evtime_starting" %><br/> Ending :<%= time_select "evtime[]", "evtime_ending" %><br/> Action: <%= text_field "evtime[]", "evtime_movement" %><br/> Notes:<br/> <%= text_area "evtime[]", "evtime_notes" %><br/> <% end %> Adrie -- 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 -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2007-Dec-18 21:49 UTC
Re: Updating HABTM records is easy, but why isn’t one_to_many ?
On 12/16/07, Adrie Donker <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Updating HABTM records is easy, but why isn''t one_to_many (has_many and > belongs_to)? > > With the collection_select it is easy to update HABTM records, everthing > is taken care of, besides declar-ing the variable for all records in de > related tabel. But updating more than one record from a belongs_to table > seems to need a lot of more coding. Or am I missing something?You''re working too hard.> Models: > class Event < ActiveRecord::Base > has_many :evtimes > endThe has many lets you get at and manipulate the list of Evtimes from the Event.> class Evtime < ActiveRecord::Base > belongs_to :event > end > > In EventsController.rb > def edit > @event = Event.find(params[:id])@evtimes = Event.evtimes> end > > def update > @event = Event.find(params[:id])@event.evtimes << Evtime.find(params[:evtimes])> if @event.update_attributes(params[:event]) > flash[:notice] = ''Event was successfully updated.'' > redirect_to :action => ''show'', :id => @event > else > render :action => ''edit'' > end > end > > And in _form.rhtml > <%= error_messages_for ''event'' %> > > <!--[form:event]--> > <p><label for="event_name">Name</label><br/> > <%= text_field ''event'', ''name'' %></p> > > <p><label for="event_city">City</label><br/> > <%= text_field ''event'', ''city'' %></p> > > <p><label for="event_description">Description</label><br/> > <%= text_field ''event'', ''description'' %></p> > > <br/> > <% for evtime in @evtimes do %> > Starting :<%= time_select ''evtime'', ''starting'' %><br/> > Ending :<%= time_select ''evtime'', ''ending'' %><br/> > Action: <%= text_field ''evtime'', ''movement''%><br/> > Notes:<br/> <%= text_area ''evtime'', ''notes'' %><br/> > <% end %> > <!--[eoform:event]-->There are better ways to do the view, e.g. using the form_for helper and using a partial for the Evtimes list, but that''s another story. You also need to handle the fact that you are editing multiple Evtimes. For that I''d direct you to RailsCasts episodes 73-75 http://railscasts.com/episodes;archive -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2007-Dec-19 00:09 UTC
Re: Updating HABTM records is easy, but why isn’t one_to_many ?
my question is that why would you have multiple event times? Why not just store the time/date of the event on the event itself? On Dec 19, 2007 8:19 AM, Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 12/16/07, Adrie Donker <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > Updating HABTM records is easy, but why isn''t one_to_many (has_many and > > belongs_to)? > > > > With the collection_select it is easy to update HABTM records, everthing > > is taken care of, besides declar-ing the variable for all records in de > > related tabel. But updating more than one record from a belongs_to table > > seems to need a lot of more coding. Or am I missing something? > > You''re working too hard. > > > > Models: > > class Event < ActiveRecord::Base > > has_many :evtimes > > end > > The has many lets you get at and manipulate the list of Evtimes from the > Event. > > > class Evtime < ActiveRecord::Base > > belongs_to :event > > end > > > > In EventsController.rb > > def edit > > @event = Event.find(params[:id]) > @evtimes = Event.evtimes > > end > > > > def update > > @event = Event.find(params[:id]) > @event.evtimes << Evtime.find(params[:evtimes]) > > > if @event.update_attributes(params[:event]) > > flash[:notice] = ''Event was successfully updated.'' > > redirect_to :action => ''show'', :id => @event > > else > > render :action => ''edit'' > > end > > end > > > > And in _form.rhtml > > <%= error_messages_for ''event'' %> > > > > <!--[form:event]--> > > <p><label for="event_name">Name</label><br/> > > <%= text_field ''event'', ''name'' %></p> > > > > <p><label for="event_city">City</label><br/> > > <%= text_field ''event'', ''city'' %></p> > > > > <p><label for="event_description">Description</label><br/> > > <%= text_field ''event'', ''description'' %></p> > > > > <br/> > > <% for evtime in @evtimes do %> > > Starting :<%= time_select ''evtime'', ''starting'' %><br/> > > Ending :<%= time_select ''evtime'', ''ending'' %><br/> > > Action: <%= text_field ''evtime'', ''movement''%><br/> > > Notes:<br/> <%= text_area ''evtime'', ''notes'' %><br/> > > <% end %> > > <!--[eoform:event]--> > > There are better ways to do the view, e.g. using the form_for helper > and using a partial for the Evtimes list, but that''s another story. > You also need to handle the fact that you are editing multiple > Evtimes. For that I''d direct you to RailsCasts episodes 73-75 > http://railscasts.com/episodes;archive > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/ > > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2007-Dec-19 01:34 UTC
Re: Updating HABTM records is easy, but why isn’t one_to_many ?
On 12/18/07, Ryan Bigg <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> my question is that why would you have multiple event times? Why not just > store the time/date of the event on the event itself?Who is to say that his Events are one-time only. He wasn''t asking about a domain question. It would be nice, by the way if you stopped top-posting. A. Because it makes the conversation hard to follow. Q. Why shouldn''t you top post? -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2007-Dec-19 01:37 UTC
Re: Updating HABTM records is easy, but why isn’t one_to_many ?
My apologies. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Adrie Donker
2007-Dec-21 18:27 UTC
Re: Updating HABTM records is easy, but why isn’t one_to_many ?
Rick, Very much thanks, that''s the code i was looking for. It''s still not easy to switch to Ruby with a 25 year RPG-background. Thanks and have a beautiful Christmas, Adrie Donker Netherlands -- 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 -~----------~----~----~----~------~----~------~--~---