multiple models questions

Hey,

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:

<objects ..>
<shout>...</shout>
<photo>...</photo>
<shout>...</shout>
...
</objects>

I can retreive these models by place_id or by profile_id!

How can I do that?

Wouter

Please can someone help me!

Wouter

How have you setup the relationships (has_many, belongs_to etc) in
your models? Or is that what you are asking how to do?

Colin

Do you *really* think that bumping after less than 24 hours will get
people to help you? (Hint: it won't.)

It will :slight_smile:

How have you setup the relationships (has_many, belongs_to etc) in
your models? Or is that what you are asking how to do?

Thank you for helping me!

Shout model:
belongs_to :place
belongs_to :profile

Photo model:
belongs_to :place
belongs_to :profile

Place model:
has_many :photos
has_many :shout

So what i want is getting a list of these 2 models combined.

It is possible (look @ http://brightkite.com/objects.xml -> also
created with ruby on rails)
<objects>
<photo..>
<note..>
<..>
</objects>

so what i want now is for example: http://mybuddynetwork.info/profiles/1-admin/objects.xml
and i get then all the photos and shouts from that user! Same for a
place (all the photos and shouts at the place).

How can i do that?

Thank you,

Wouter

Do you *really* think that bumping after less than 24 hours will get
people to help you? (Hint: it won't.)

It will :slight_smile:

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?

Thank you for helping me!

Shout model:
belongs_to :place
belongs_to :profile

Photo model:
belongs_to :place
belongs_to :profile

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 @ http://brightkite.com/objects.xml -> also
created with ruby on rails)
<objects>
<photo..>
<note..>
<..>
</objects>

so what i want now is for example: http://mybuddynetwork.info/profiles/1-admin/objects.xml
and i get then all the photos and shouts from that user! Same for a
place (all the photos and shouts at the place).

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

>> Do you *really* think that bumping after less than 24 hours will get
>> people to help you? (Hint: it won't.)

> It will :slight_smile:

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?

> Thank you for helping me!

> Shout model:
> belongs_to :place
> belongs_to :profile

> Photo model:
> belongs_to :place
> belongs_to :profile

> 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 @http://brightkite.com/objects.xml-> also
> created with ruby on rails)
> <objects>
> <photo..>
> <note..>
> <..>
> </objects>

> so what i want now is for example:http://mybuddynetwork.info/profiles/1-admin/objects.xml
> and i get then all the photos and shouts from that user! Same for a
> place (all the photos and shouts at the place).

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

How can i do this?

And thank you for helping me!
Wouter

Is that the real question then? It was not clear to me.

I have not used to_xml, but a glance at the docs at
http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html
suggests that associated models can be included by using :include, or
have you not managed to get that to work?

Colin

Yes that is the question :smiley:

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?

Wouter

Yes that is the question :smiley:

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?

No idea, sorry. As I said I have not used to_xml

Colin

[snip]

You should probably use includes or joins in your statement so that you
are pulling all the data in together..

Any idea how I can do that? How can i use that?

Thank you for your answer!

@shouts = Shout.find(:all, :include => [:place, :profile])
@photos = Photo.find(:all, :include => [:place, :profile])

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:

<?xml version="1.0" encoding="UTF-8"?>
<arrays type="array">
  <array type="array">
    <array>
      <body>Ahaaa</body>
      <created_at type="datetime">2009-08-14T15:56:40Z</created_at>
      <id type="integer">71</id>
      <place_id type="integer">51</place_id>

      <profile_id type="integer">1</profile_id>
    </array>
  </array>
  <array type="array">
    <array>
      <caption></caption>
      <created_at type="datetime">2009-08-14T17:14:55Z</created_at>
      <geo_lat type="float">50.7727</geo_lat>

      <geo_long type="float">4.53766</geo_long>
      <id type="integer">20</id>
      <image>/Users/woutergo/Documents/workspace/myBuddyNetwork/public/
system/photo/image/20/14njk8w.jpg</image>
      <place_id type="integer">51</place_id>
      <profile_id type="integer">1</profile_id>
      <show_geo type="integer" nil="true"></show_geo>

      <updated_at type="datetime">2009-08-14T17:14:55Z</updated_at>
    </array>
  </array>
</arrays>

There must be another way.. Thank you

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.

Hey if you see at my example above:

<array>
      <body>Ahaaa</body>
      <created_at type="datetime">2009-08-14T15:56:40Z</created_at>
      <id type="integer">71</id>
      <place_id type="integer">51</place_id>

      <profile_id type="integer">1</profile_id>
    </array>
==> SHOUT

<array>
      <caption></caption>
      <created_at type="datetime">2009-08-14T17:14:55Z</created_at>
      <geo_lat type="float">50.7727</geo_lat>

      <geo_long type="float">4.53766</geo_long>
      <id type="integer">20</id>
      <image>/Users/woutergo/Documents/workspace/myBuddyNetwork/
public/
system/photo/image/20/14njk8w.jpg</image>
      <place_id type="integer">51</place_id>
      <profile_id type="integer">1</profile_id>
      <show_geo type="integer" nil="true"></show_geo>

      <updated_at type="datetime">2009-08-14T17:14:55Z</updated_at>
    </array>

==> PHOTO

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!

Thank you for your effort!

Wouter

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

Colin Law wrote:

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..

No sorry I dont need to make a view.
I have this already for shouts and photos.

I only need to find a way to make the corrent xml file like i posted
already!

Do i have to use XML builder to make the xml like i want?

Please has no one an idea? It must be possible with ruby on rails..

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.

Colin

Hey,

No I havent tried this, how do i have to do this? Can you give me an
example?

Thank you

Wouter