I have a small social network app and i am creating a kind of activity
stream for a user.
A user can post shouts and photos (models Shout and Photo).
These 2 models have a profile_id and place_id (where shout is posted
or photo is uploaded).
What i want to do now is get an array of these 2 models (objects) to
make an xml file like this:
Do you *really* think that bumping after less than 24 hours will get
people to help you? (Hint: it won't.)
It will
No it didn't, if you notice I replied to your original not the bump.
It was just coincidence. An apology would have been a better response
than that above. It is not good to antagonise those that you hope
will help you.
How have you setup the relationships (has_many, belongs_to etc) in
your models? Or is that what you are asking how to do?
If you have a place object then the photos are accessible as
place.photos and the shouts in place.shouts. These will return arrays
of Places and Shouts.
>> Do you *really* think that bumping after less than 24 hours will get
>> people to help you? (Hint: it won't.)
> It will
No it didn't, if you notice I replied to your original not the bump.
It was just coincidence. An apology would have been a better response
than that above. It is not good to antagonise those that you hope
will help you.
Yes i know it was coincidence! Sorry for my answer..
>> How have you setup the relationships (has_many, belongs_to etc) in
>> your models? Or is that what you are asking how to do?
> Place model:
> has_many :photos
> has_many :shout
This should be has_many :shouts
> So what i want is getting a list of these 2 models combined.
> It is possible (look @Brightkite Universe - brightkite.com; also
> created with ruby on rails)
> <objects>
> <photo..>
> <note..>
> <..>
> </objects>
If you have a place object then the photos are accessible as
place.photos and the shouts in place.shouts. These will return arrays
of Places and Shouts.
Colin
Hey,
But this will make 2 seperate arrays or am i wrong..
i want to have place.shouts and place.photos in the same array..
so like @objects = place.photos and place.shouts (but that don't work
off course)
and then i do @objects.to_xml
i have used include but is not a good option for me.. it includes the
model (object) in the child of the xml file like this
<objects>
<otherobject>
<shout></shout>
</otherobject>
</objects>
I managed now to get an array of the shouts and photos like this
@objects = [@place.shouts, @place.photos]
and i get the objects in the xml file but the xml is like this now:
<array type="array">
-<array type="array">
--<array>
<body>..
THIS IS PHOTO
--</array>
-<array type="array">
--<array>
<body>..
SHOUT
--</array>
so i get first the shouts and then the photos and this again in an
array.. not how i wanted.. and it has to be sorted on date and not
first the photos and then the shouts..
Any idea how to solve that?
What is, I don't see any question above. Oh, you mean the one half a
mile below?
i have used include but is not a good option for me.. it includes the
model (object) in the child of the xml file like this
<objects>
<otherobject>
<shout></shout>
</otherobject>
</objects>
I managed now to get an array of the shouts and photos like this
@objects = [@place.shouts, @place.photos]
and i get the objects in the xml file but the xml is like this now:
<array type="array">
-<array type="array">
--<array>
<body>..
THIS IS PHOTO
--</array>
-<array type="array">
--<array>
<body>..
SHOUT
--</array>
so i get first the shouts and then the photos and this again in an
array.. not how i wanted.. and it has to be sorted on date and not
first the photos and then the shouts..
Any idea how to solve that?
But with this i got 2 arrays and I need 1 array with all the photos
and shouts in it..
and if i do @objects = [@shouts, @photos] and then @objects.to_xml, I
get the same as a few posts above:
You can still just use one request, but you need to build your
associations better. I'm not sure exactly what you have in each and
every table but something you should consider here is not to
over-normalize your db/tables too much.
For instance, if one of those tables just contains one or two columns,
does it have to be a separate table? Or, can combine it with another
table? If you need to have separate foreign keys for specific areas of
a table, you can look at self-referential joins.
so in these 2 objects there is a profile_id and a place_id. I only
need to have these 2 objects combined per place or per profile (sorted
by profile_id or by place_id)!
A place has photos and shouts and a profile has photos and shouts..
I am creating an android Application and for this i need to make one
request with these 2 objects combined in 1 xml file.
I hope someone can help me, really need to find this!
It took me a little while to realise that it is not fetching the data
that is the OPs problem but how to use to_xml to generate his xml in
the way he wants.
It took me a little while to realise that it is not fetching the data
that is the OPs problem but how to use to_xml to generate his xml in
the way he wants.
Colin
The problem is two fold:
Controller
def my_xml
@shouts = Shout.find(:all, :include => [:place, :profile])
respond_to do |format|
format.html
format.xml { render :xml, :xml => @shouts.to_xml }
end
end
View
<ul>
<% for shout in @shouts-%>
<li>
<%=h shout.something %>, <%=h shout.place.something %>, <%=h
shout.profile.something %>
</li>
</ul>
You use render to build the XML.
However, you still need to work on associations so you can make use of
includes/joins to make just one array..
Have you tried providing some virtual members of your model that make
that data accessible in the way that you want, then using to_xml and
specifying only the virtual members to be used? I don't know if this
will work but it is worth a try.