2

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

2
  • please explain more what means: "to keep only one array with the objects returned from 3 models" Commented Jul 20, 2011 at 8:22
  • I think your sorting problem should go into another question. (Otherwise try with order('created_on')...) Commented Jul 20, 2011 at 8:40

3 Answers 3

5

You're using the wrong operator, you want this:

@final = @blogs + @wikis + @medias

The << operator for an Array:

Pushes the given object on to the end of this array.

whereas the + operator:

Returns a new array built by concatenating the two arrays together to produce a third array.

You could also use flatten if you were really attached to << for some reason:

@final = (@final << @blogs << @wikis << @medias).flatten

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 @medias arrays.

As far as sorting them goes, you could use sort_by!:

@final.sort_by(&:created_at)

That assumes that you have the usual created_at method on the objects.

Sign up to request clarification or add additional context in comments.

6 Comments

@mu is too short -- Hi i tried with + operator it worked . But how to list them based on the creation date of each object
@Jasmine: Sorry, I missed that part of your question. I've added an update to cover it.
What is the content of @blogs after final = (@blogs << @wikis << @medias).flatten? I think, this statement will lead to @blogs = [ blog1, blog2, ..., [ wiki1, wiki2, ... [ media1, media2, ... ]]]
@powerMicha: Yes, that will modify @blogs but @final will be right, I'll add an update to sort that out. Thanks.
@mu is too short -- How to apply the limit in the final array ??
|
2

Try Array.flatten.

Returns a new array that is a one-dimensional flattening of this array (recursively).

Comments

2

With << you are putting your arrays into an array. This is the same like:

@final = [@blogs, @wikis, @medias]

I suggest mu is too short's answer to join your arrays

@final = @blogs + @wikis + @medias

See API

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.