#94
Feb 24, 2008

ActiveResource Basics

ActiveResource allows you to easily communicate between multiple Rails applications. See how in this episode.
Download (15.6 MB, 7:07)
alternative download for iPod & Apple TV (9.6 MB, 7:07)
# models/product.rb
class Product < ActiveResource::Base
  self.site = "http://localhost:3000"
end

# models/post.rb
class Post < ActiveRecord::Base
  def product
    @product ||= Product.find(product_id) unless product_id.blank?
  end
end
<!-- views/posts/edit.html.erb -->
<p>
  <%= f.label :product_id %>
  <%= f.collection_select :product_id, Product.find(:all), :id, :name %>
</p>

<!-- views/posts/show.html.erb -->
<% if @post.product %>
  <strong><%=h @post.product.name %></strong>
<% end %>

19 comments

Johannes Feb 25, 2008 at 00:15

Thanks for that episode.
Just yesterday I was thinking about how to do the communication between two rails applications. So this is a really big help for me :-)

I'm looking forward to the next episode on this topic :-)


Martin Feb 25, 2008 at 00:58

I also think that ActiveResource is a great way to access remote data. In fact I am using it to store and access data in Amazon's SimpleDB webservice.

If anybody wants to dig into this, check out my little Tutorial over at the Amazon AWS site:

http://developer.amazonwebservices.com/connect/entry.jspa?categoryID=152&externalID=1242


Phil Feb 25, 2008 at 01:56

Does ActiveResource know that its looking for a Product controller because its called Product.rb? what if you already had a product model? how could you tell ActiveResorce to look for a product controller at that site?


Alexander Lomakin Feb 25, 2008 at 05:34

Thanks! It's useful.


Frankie Roberto Feb 26, 2008 at 02:07

This is pretty cool - I hadn't heard of ActiveResource before.


Fred Feb 26, 2008 at 07:24

Wow, that was really interesting.

It's good to know it can be so easily implemented.

Thanks once again Ryan.


Mike Feb 26, 2008 at 08:14

Rails is so cool!

Ryan, do you want to do a railscast about TimeZones like TzTime or the new edge zone?


oin Feb 26, 2008 at 10:48

Could you please, pretty please use some other program to encode your videos? I don't own a Mac and every player I try to play your screencasts with fails to read its index and so I can't


oin Feb 26, 2008 at 10:50

seek inside the video at all. I tried mplayer/xine/vlc on Linux


Ryan Bates Feb 26, 2008 at 15:11

@Phil, good question, what if you already have a Product model? Looking at the source it appears it's possible to set the element_name or collection_name like this in the class:

self.element_name = 'product'

That way you can give the class another name such as RemoteProduct.

@oin, I'll look into providing the movies in an alternative (flash?) format. Thanks for the feedback.


drozzy Feb 27, 2008 at 06:26

Hello Ryan,
I enjoyed immensely your railcasts. I've watched all of them over the course of last 3 days.
Is it just me - or do you seem more stressed in a later railcasts?


Ryan Bates Feb 28, 2008 at 09:09

@drozzy, I suppose I've been a little stressed lately. But good news is I'm off work this week so I have time to clear off the stuff that's been building up on my plate. Thank for your concern.


oin Feb 29, 2008 at 05:29

@Ryan: I don't think the format is the problem, since I can seek perfectly in other .movs or .m4vs, but not yours

I forgot to say: thanks for the great railscasts!


Ryan Bates Feb 29, 2008 at 11:09

@oin, that's very strange. I'm using the Animation codec for the .mov files, so that might be a problem. The .m4v is H.264 so I would imagine that would work. I exported them using QuickTime Pro.


Stephen Mar 01, 2008 at 09:55

Ryan - thanks again!
You have a knack for making episodes about exactly the problem I'm tackling.


jhkim Mar 05, 2008 at 18:02

Cool!! Thanks a lot.


Javier Apr 02, 2008 at 09:55

Ryan, you just saved my day with Railscast #94 and #95! For HOURS I was looking for a short and neat little howto in ActiveResource as the one you made. Muchas gracias! :-)


kino May 03, 2008 at 07:26

Thanx! You are helped


руби разработчик Jul 20, 2008 at 07:18

Where this possible uses?

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(required)

subscribe:
sponsored by:
if you want to help:
required:
Get Quicktime Player