First, thanks to everyone who replied to my pluralization question from before! Today I was trying to do what I think is called a "chained accessor". I have an "events" table and each event has one user. Instead of seeing the id number for each event''s user, I''d rather display the names of users instead. I tried to do this: class Event < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :events end in the event_controller.rb: def list @users = User.find(:all) @events = Event.find(:all) end in the list.rhtml: <% @events.each do |eachevent| %> <tr> <td><%= link_to eachevent.action, :action => "show", :id => eachevent.id %> <TD><%= eachevent.user.name %></TD> <td><%= eachevent.whenit %> </td> </tr> <% end %> But I get an error saying "You have a nil object when you didn''t expect it! The error occured while evaluating nil.name" What am I doing wrong? Thanks in advance for all replies! -Jason
Hey, i think in your controller, you just need def list @events = Event.find_all end then in your view, <% @events.each {|event| %> <%= event.user.name %> <% } %> if that still breaks, which it probly will, then you may just not have a user associated with it. you could add this to test: <% unless event.user.name %> eek, you dont even got no user associated with this event <% end %> then i''d check the table contents and make sure you have the right values in there. hope it helps, d. Jason Frankovitz wrote:> First, thanks to everyone who replied to my pluralization question > from before! > > Today I was trying to do what I think is called a "chained accessor". > I have an "events" table and each event has one user. Instead of > seeing the id number for each event''s user, I''d rather display the > names of users instead. I tried to do this: > > class Event < ActiveRecord::Base > belongs_to :user > end > > class User < ActiveRecord::Base > has_many :events > end > > in the event_controller.rb: > def list > @users = User.find(:all) > @events = Event.find(:all) > end > > in the list.rhtml: > <% @events.each do |eachevent| %> > <tr> > <td><%= link_to eachevent.action, :action => "show", :id => > eachevent.id %> > <TD><%= eachevent.user.name %></TD> > <td><%= eachevent.whenit %> </td> > </tr> > <% end %> > > But I get an error saying "You have a nil object when you didn''t > expect it! The error occured while evaluating nil.name" > > What am I doing wrong? Thanks in advance for all replies! > > -Jason > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
> Jason Frankovitz wrote:>> >> in the list.rhtml: >> <% @events.each do |eachevent| %> >> <tr> >> <td><%= link_to eachevent.action, :action => "show", :id => >> eachevent.id %> >> <TD><%= eachevent.user.name %></TD> >> <td><%= eachevent.whenit %> </td> >> </tr> >> <% end %> >> >> But I get an error saying "You have a nil object when you didn''t >> expect it! The error occured while evaluating nil.name" >>> you could add this to test: > > <% unless event.user.name %> > eek, you dont even got no user associated with this event > <% end %>You''re still going to raise an exception here if an event doesn''t have a user. I think you meant to write: <% unless event.user %> eek, you dont even got no user associated with this event <% end %> -- R.Livsey http://livsey.org
I have 2 tables ----------------- cursos id name -------------------- turmas id cursos_id name Model turma belongs_to :cursos Model curso has_manay :turma TurmasController scaffold:turma Ok, it''s works, but I can''t select, the Curso in curso ID field, it''s only display the field name... what can be wrong? -- Posted via http://www.ruby-forum.com/.
On 6/28/06, Fernando <fernando@setti.com.br> wrote:> I have 2 tables > > ----------------- > cursos > id > name > > -------------------- > turmas > id > cursos_id > name > > > > Model turma > > belongs_to :cursos > > Model curso > > has_manay :turmahas_many :turmas
not working yeat .................. -------------- Table Cursos id name ------------------- Table Turmas id name cursos_id --------------------- Model Turma class Turma < ActiveRecord::Base belongs_to :cursos end -------------------------- Model Curso class Curso < ActiveRecord::Base has_many :turmas end ------------------------------- turmas_controller class TurmasController < ApplicationController scaffold:turma end ------------------------------------- When I click in the link NEW TURMA the form display only 1 field.. the name field. And my Cursos Table is not empty. -- Posted via http://www.ruby-forum.com/.
I''ve come in late to this discussion, so I haven''t seen what messages have already been sent back and forth. Sorry if I''m going to cover anything that''s already been brought up... On Jun 28, 2006, at 12:26 PM, Fernando wrote:> not working yeat > .................. > -------------- > Table Cursos > id > name > ------------------- > Table Turmas > id > name > cursos_id > --------------------- > Model Turma > class Turma < ActiveRecord::Base > belongs_to :cursos > end > -------------------------- > Model Curso > class Curso < ActiveRecord::Base > has_many :turmas > end > ------------------------------- > turmas_controller > class TurmasController < ApplicationController > scaffold:turma > end > ------------------------------------- > > When I click in the link NEW TURMA > the form display only 1 field.. > > the name field. > > And my Cursos Table is not empty.What you are seeing is not an unexpected result. Unfortunately, you are using the scaffold controller method, and not a fully generated scaffold, from a call to: ./script/generate scaffold Turma If you were to generate the scaffold, you would see that when the new (also the edit) form is built that it only contains entry fields for those columns that a user would normally enter data into. Rails calls these the "content columns" of a given ActiveRecord model class. ID columns, whether they are primary key fields (ie. id) , or foreign key fields (ie. cursos_id) are not considered content columns by Active Record. Additionally, the built in scaffolding is only for building a basic CRUD structure for a single model class. There are no facilities, though the build-in scaffolding, to do anything with associated tables. This is why what you are seeing is, in fact, what you are supposed to be seeing. -Brian