ruby on rails - undefined method `location' for #<Event::ActiveRecord_Associations_CollectionProxy:0x007f834658ea50> -


i have 2 models: event , location. location has_many events , event belongs_to location. location has attribute called "city". in controller, have following code:

@locations = location.all.includes(:events) @events = @locations.collect{|l| l.events} @events.each |event|     puts "event #{event.inspect}"     puts "event location #{event.location}" end 

i getting following error:

undefined method `location' #<event::activerecord_associations_collectionproxy:0x007f834658ea50> 

i know events have location, should work. how work?

this fail if of locations has no events associated it. can try ensure have no nil elements in array:

@locations = location.all.includes(:events) @events = @locations.collect{|l| l.events}.   flatten.   reject{|e| e.nil?} @events.each |event|     puts "event #{event.inspect}"     puts "event location #{event.location}" end 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -