I am working in rails 2.. I have a doubt.
In my application , i have 3 models like
Blog, Wiki , Media
I am trying to create an array like @final with all the blogs , wikis, medias posted under my application .
I have tried with
@blogs = Blog.all
@wikis = Wiki.all
@medias = Media.all
@final = []
@final << @blogs << @wikis << @medias
The above final array has 3 arrays in it..
But i am expecting to keep final array with the objects returned from 3 models
How to do so ??
PLease give suggestions
EDIT
I thing i tried is
@final = []
@blogs = Blog.all
@wikis = Wiki.all
@medias = Media.all
@final = @blogs + @wikis + @medias
This is performing exactly what i need .. But its just listing all the blogs , medias and wikis. How to list all the entities based on the creation date of the particular object
You’re using the wrong operator, you want this:
The
<<operator for an Array:whereas the
+operator:You could also use
flattenif you were really attached to<<for some reason:but that would be a bit pointless. As noted by powerMicha, you need to start the
<<chain off with@final(or some temporary array) if you don’t want to modify any of the@blogs,@wikis, or@mediasarrays.As far as sorting them goes, you could use
sort_by!:That assumes that you have the usual
created_atmethod on the objects.