I''m trying to read the documentation on associations instead of using piggy back calls... http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html I have a series of children inside a parent and want to list them on the show.rhtml for the parent. here''s teh show.rhtml for sequence: <% for column in Sequence.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @sequence.send(column.name) %> </p> <% end %> <%= link_to ''Edit Sequence'', :action => ''edit'', :id => @sequence %> | <%= link_to ''Back'', :action => ''list'' %> <hr /> <p><b>Shots in <%=h @sequence["name"] %> Sequence</b></p> <%= for shots in shot.find(:all, :include => :sequence_id) %> The last line is what i''m getting errors on. I''ve tried a variety of things and all of them come back with a generic syntax error... -- John Athayde bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org Meticulous | www.meticulous.com (work) Rotoscope | www.rotoscope.com (sound: rock band) Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic) Personal Weblog | www.boboroshi.com (play) "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin (1706-1790) Reply of the Pennsylvania Assembly to the Governor November 11, 1755 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 7/18/05, John Athayde <bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org> wrote:> <hr /> > > <p><b>Shots in <%=h @sequence["name"] %> Sequence</b></p> > > <%= for shots in shot.find(:all, :include => :sequence_id) %>^ -> remove this "=", and remember to terminate with a <% end %>
Hmm. No dice. I was trying something a bit more complex pehaps. I have a url such as this: http://localhost:3000/sequences/show/1 I want to pull the ID at the end for the sequence (referenced in the shot table as sequence_id) In the sequence controller.rb i have this: def show @sequence = Sequence.find(params[:id]) @shots = Shot.shots_in_sequence end in the shot.rb model i have def self.shots_in_sequence sequenceid = @Sequence[''id''] find(:all, :conditions => "sequence_id = ?", sequenceid) end and in the show.rhtml for sequence i have <% for shot in @shots %> <p> <b><%= shot.number %></b> | <%=h shot.name %> </p> <% end %> This generates an error of: SyntaxError in Sequences#show /app/models/shot.rb:12: syntax error (eval):5:in `has_many'' /app/models/status.rb:2 (eval):5:in `belongs_to'' /app/models/sequence.rb:3 /app/controllers/sequences_controller.rb:12:in `show'' script/server:49 When I test by actually calling the ID in the conditions in shot.rb, such as this: def self.shots_in_sequence sequenceid = @Sequence[''id''] find(:all, :conditions => "sequence_id = 1") end I get this error: NoMethodError in Sequences#show Showing /sequences/show.rhtml where line #13 raised: WARNING: You have a nil object when you probably didn''t expect it! Odds are you want an instance of Array instead. Look in the callstack to see where you''re working with an object that could be nil. Investigate your methods and make sure the object is what you expect! I''m trying to follow the examples in teh Pragmatic Programmers book, but having little luck with this one... unless i call a sequence with no shots, in which case it renders fine. On Jul 19, 2005, at 12:02 AM, Cuong Tran wrote:> On 7/18/05, John Athayde <bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org> wrote: > >> <hr /> >> >> <p><b>Shots in <%=h @sequence["name"] %> Sequence</b></p> >> >> <%= for shots in shot.find(:all, :include => :sequence_id) %> >> > ^ -> remove this "=", and remember to terminate with a <% > end %> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- John Athayde bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org Meticulous | www.meticulous.com (work) Rotoscope | www.rotoscope.com (sound: rock band) Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic) Personal Weblog | www.boboroshi.com (play) "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin (1706-1790) Reply of the Pennsylvania Assembly to the Governor November 11, 1755
:conditions =>["sequence_id = ?", sequenceid] On 7/18/05, John Athayde <bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org> wrote:> :conditions => "sequence_id = ?", sequenceid
a h a. okay. I''ve got this: def self.shots_in_sequence find(:all, :conditions => "shots.sequence_id like ''%1%''" ) end And it pulls the items that match 1. Now how do i get the %1% to be the value of the page i''m viewing? (sequence.id = 1) On Jul 19, 2005, at 12:57 AM, John Athayde wrote:> Hmm. No dice. > > I was trying something a bit more complex pehaps. I have a url such > as this: > > http://localhost:3000/sequences/show/1 > > I want to pull the ID at the end for the sequence (referenced in > the shot table as sequence_id) > > In the sequence controller.rb i have this: > > def show > @sequence = Sequence.find(params[:id]) > @shots = Shot.shots_in_sequence > end > > in the shot.rb model i have > > def self.shots_in_sequence > sequenceid = @Sequence[''id''] > find(:all, > :conditions => "sequence_id = ?", sequenceid) > end > > and in the show.rhtml for sequence i have > > <% for shot in @shots %> > <p> > <b><%= shot.number %></b> | <%=h shot.name %> > </p> > <% end %> > > This generates an error of: > > SyntaxError in Sequences#show > > /app/models/shot.rb:12: syntax error > (eval):5:in `has_many'' > /app/models/status.rb:2 > (eval):5:in `belongs_to'' > /app/models/sequence.rb:3 > /app/controllers/sequences_controller.rb:12:in `show'' > script/server:49 > > > > When I test by actually calling the ID in the conditions in > shot.rb, such as this: > > def self.shots_in_sequence > sequenceid = @Sequence[''id''] > find(:all, > :conditions => "sequence_id = 1") > end > > > I get this error: > > NoMethodError in Sequences#show > > Showing /sequences/show.rhtml where line #13 raised: > > WARNING: You have a nil object when you probably didn''t expect > it! Odds are you > want an instance of Array instead. > > Look in the callstack to see where you''re working with an object > that could be nil. > Investigate your methods and make sure the object is what you expect! > I''m trying to follow the examples in teh Pragmatic Programmers > book, but having little luck with this one... > > > unless i call a sequence with no shots, in which case it renders fine. > > On Jul 19, 2005, at 12:02 AM, Cuong Tran wrote: > > >> On 7/18/05, John Athayde <bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org> wrote: >> >> >>> <hr /> >>> >>> <p><b>Shots in <%=h @sequence["name"] %> Sequence</b></p> >>> >>> <%= for shots in shot.find(:all, :include => :sequence_id) %> >>> >>> >> ^ -> remove this "=", and remember to terminate with a <% >> end %> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > -- > > John Athayde > bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org > > Meticulous | www.meticulous.com (work) > Rotoscope | www.rotoscope.com (sound: rock band) > Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic) > Personal Weblog | www.boboroshi.com (play) > > "Those who would give up essential Liberty, to purchase a little > temporary Safety, deserve neither Liberty nor Safety." > - Benjamin Franklin (1706-1790) > Reply of the Pennsylvania Assembly to the > Governor > November 11, 1755 > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- John Athayde bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org Meticulous | www.meticulous.com (work) Rotoscope | www.rotoscope.com (sound: rock band) Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic) Personal Weblog | www.boboroshi.com (play) "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin (1706-1790) Reply of the Pennsylvania Assembly to the Governor November 11, 1755 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
That also returns a nil object. On Jul 19, 2005, at 1:28 AM, Robby Lansaw wrote:> :conditions =>["sequence_id = ?", sequenceid] > > On 7/18/05, John Athayde <bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org> wrote: > >> :conditions => "sequence_id = ?", sequenceid >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- John Athayde bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org Meticulous | www.meticulous.com (work) Rotoscope | www.rotoscope.com (sound: rock band) Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic) Personal Weblog | www.boboroshi.com (play) "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin (1706-1790) Reply of the Pennsylvania Assembly to the Governor November 11, 1755 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I think you want something like this: in app/models/shot.rb: class Shot <...... belongs_to :sequence end in app/models/sequence.rb: class Sequence <...... has_many :shots end in your controller: def show @sequence = Sequence.find(params[:id], :include => [:shots]) end in your view: <% @sequence.shots.each do |shot| %> <p> <b><%= shot.number %></b> | <%=h shot.name %> </p> <% end %> On Tue, 2005-07-19 at 00:57 -0400, John Athayde wrote:> Hmm. No dice. > > I was trying something a bit more complex pehaps. I have a url such > as this: > > http://localhost:3000/sequences/show/1 > > I want to pull the ID at the end for the sequence (referenced in the > shot table as sequence_id) > > In the sequence controller.rb i have this: > > def show > @sequence = Sequence.find(params[:id]) > @shots = Shot.shots_in_sequence > end > > in the shot.rb model i have > > def self.shots_in_sequence > sequenceid = @Sequence[''id''] > find(:all, > :conditions => "sequence_id = ?", sequenceid) > end > > and in the show.rhtml for sequence i have > > <% for shot in @shots %> > <p> > <b><%= shot.number %></b> | <%=h shot.name %> > </p> > <% end %> > > This generates an error of: > > SyntaxError in Sequences#show > > /app/models/shot.rb:12: syntax error > (eval):5:in `has_many'' > /app/models/status.rb:2 > (eval):5:in `belongs_to'' > /app/models/sequence.rb:3 > /app/controllers/sequences_controller.rb:12:in `show'' > script/server:49 > > > > When I test by actually calling the ID in the conditions in shot.rb, > such as this: > > def self.shots_in_sequence > sequenceid = @Sequence[''id''] > find(:all, > :conditions => "sequence_id = 1") > end > > > I get this error: > > NoMethodError in Sequences#show > > Showing /sequences/show.rhtml where line #13 raised: > > WARNING: You have a nil object when you probably didn''t expect it! > Odds are you > want an instance of Array instead. > > Look in the callstack to see where you''re working with an object that > could be nil. > Investigate your methods and make sure the object is what you expect! > I''m trying to follow the examples in teh Pragmatic Programmers book, > but having little luck with this one... > > > unless i call a sequence with no shots, in which case it renders fine.-- Dan Willemsen <dan-6hMTLc6QzRdmISeLE6Wk8g@public.gmane.org>
Dan - Thanks! This worked great. Now to figure out why it worked... On Jul 19, 2005, at 2:07 AM, Dan Willemsen wrote:> I think you want something like this: > > in app/models/shot.rb: > class Shot <...... > belongs_to :sequence > end > > in app/models/sequence.rb: > class Sequence <...... > has_many :shots > end > > in your controller: > def show > @sequence = Sequence.find(params[:id], :include => [:shots]) > end > > in your view: > <% @sequence.shots.each do |shot| %> > <p> > <b><%= shot.number %></b> | <%=h shot.name %> > </p> > <% end %> >-- John Athayde bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org Meticulous | www.meticulous.com (work) Rotoscope | www.rotoscope.com (sound: rock band) Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic) Personal Weblog | www.boboroshi.com (play) "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin (1706-1790) Reply of the Pennsylvania Assembly to the Governor November 11, 1755 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails