So, I have a bunch of users, everyone has user.dj_name attributes. This is a confirmed need for a model, but I'm still careful here because I have problems.
I want to get a bunch of users, and then order them by their dj_name. Something like that:
@djs = Event.all.map { |e| e.program.user }.sort_by {|x,y| x.dj_name <=> y.dj_name }
where he pulls all the DJs who have events (shows). It does not work with NoMethodError: undefined `dj_name 'method for nil: NilClass
So I tried:
@djs = Event.all.map { |e| e.program.user } @djs.compact.sort_by! {|x,y| x.dj_name <=> y.dj_name rescue nil}
And it does not sort. Without the "rescue nil" clause, I get the same error.
And if I make a refusal! if the object is zero, I get nothing.
> @djs.reject! {|d| d.nil? } => nil
It seems that none of the objects in the array is equal to zero, the sorting mechanism gives me errors, and saving it just stops the sorting process and returns an immutable array.
halp?
source share