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




I can retreive these models by place_id or by profile_id!

How can I do that?

Wouter

Please can someone help me!

Wouter

Wouter wrote:

Please can someone help me!

Wouter

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

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

2009/8/13 Wouter [email protected]:

make an xml file like this:
How can I do that?

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 @ Brightkite Universe - brightkite.com → also
created with ruby on rails)

<photo…>
<note…>
<…>

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

On 14 aug, 18:21, Colin L. [email protected] wrote:

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…

Photo model:

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

2009/8/14 Wouter [email protected]:

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.

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

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




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:

- -- .. THIS IS PHOTO -- - -- .. SHOUT --

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

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

2009/8/14 Wouter [email protected]:

[snip]
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?

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

Wouter wrote:

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

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

You can also use a :joins in the model if you tie it all together
properly. This is just a very basic example.

Test it out in the rails console then call the data back…

shouts = Shout.find(:all, :include => [:place, :profile])
shouts.place
shouts.profile
etc…

2009/8/14 Wouter [email protected]:

Yes that is the question :smiley:

What is, I don’t see any question above. Oh, you mean the one half a
mile below?

@objects = [@place.shouts, @place.photos]
  …
  SHOUT

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]

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"?> Ahaaa 2009-08-14T15:56:40Z 71 51
  <profile_id type="integer">1</profile_id>
</array>
2009-08-14T17:14:55Z 50.7727
  <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
<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>

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.

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

2009/8/14 Alpha B. [email protected]:

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 L. wrote:

2009/8/14 Alpha B. [email protected]:

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

    <% for shout in @shouts-%>
  • <%=h shout.something %>, <%=h shout.place.something %>, <%=h shout.profile.something %>

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…

Hey if you see at my example above:

Ahaaa 2009-08-14T15:56:40Z 71 51
  <profile_id type="integer">1</profile_id>
</array>

==> SHOUT

2009-08-14T17:14:55Z 50.7727
  <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
<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

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

On 17 aug, 22:05, Colin L. [email protected] wrote:

 <created_at type="datetime">2009-08-14T15:56:40Z</created_at>
 <geo_lat type="float">50.7727</geo_lat>

Colin
Hey,

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

Thank you

Wouter