I tried to work out an active resource example with complex
relationships between records and had not much luck so I gave up and
manipulate *_id fields myself to get around this in a somewhat cludgy
way at times. I am wondering if there are any good books with examples
on this ? I have the agile web book 2, does book 3 go into this in
greater depth ?
You might take a look at: http://guides.rubyonrails.org/
Once you get there follow the links to the different Model topics, in
particular check out:
What I mean is active resource over a web service so that you run a
ruby script outside of rails remotely and access the active record
objects in the rails service over http as active resource provides and
still have the complex relationships, that second part I have found
tricky
Right. You might want to look at Chap 15 in “The Rails Way”, Obie
Fernandez et.al., 2008. It covers XML and ActiveResource - only 27
pages but the best I’ve been able to find to date. I’m assuming that
what you mean by “complex relationships” is the A…Resource parallel
to associations in A…Record. If not, clue me in with more details.
I’ve had that book ever since it came out, and have tried to work on
this problem using the book as well.
What I seem to recall is that belongs_to, has_many, or anything
similar is not that easy to figure out how to get it to work, at least
I have not had good luck with it.
I find it easier that when the rails server calls a drb process and on
some other machine on the network and that process needs to use active
resource, I just pass an array of record id’s through drb and then drb
can do rec.find(id) which works fine. Then if I have a relationship, I
just handle it manually by setting say rec.parent_id. Other types of
find() may cause the server to send over all the records for it to
search through which can be a problem if there are too many records.
This seems kludgy, but that’s what I have been doing ever since I
gave up on trying to figure this out as it seemed there where not
enough good examples or those that I found I had problems getting to
work.
what’s been difficult about using active record?
can you post snippets of your current code and then maybe we can
rewrite it using AR?
-Gabe
I’m not sure I understand what you’re trying to accomplish but here’s
instructions for a has_and_belongs_to_many example which also provides
an ActiveResource (xml) feed.
Assumptions:
Ruby 1.8.7
Rails 2.3.2
sqlite3
-
run:
rails simple_test
cd simple_test -
run:
script/generate scaffold User name:string
script/generate scaffold Like name:string
script/generate migration LikesUsers -
edit the models user.rb and like.rb to read:
class User < ActiveRecord::Base
has_and_belongs_to_many :likes
endclass Like < ActiveRecord::Base
has_and_belongs_to_many :users
end -
edit db/migrate/*_likes_users.rb to read:
class LikesUsers < ActiveRecord::Migration
def self.up
create_table :likes_users, :id => false do |t|
t.column :like_id, :integer, :null => false
t.column :user_id, :integer, :null => false
end
end
def self.down
drop_table :likes_users
end
end
- edit the users and likes controllers’ show methods
to read:
GET /users/1
GET /users/1.xml
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user.to_xml(:include => :likes) }
end
end
GET /likes/1
GET /likes/1.xml
def show
@like = Like.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @like.to_xml(:include => :users) }
end
end
-
run:
rake db:migrate
script/server -
run:
script/console
Loading development environment (Rails 2.3.2)fred = User.create(:name => “Fred”)
=> #<User id: 1, name: “Fred”, created_at: “2009-06-24 16:13:42”,
updated_at: “2009-06-24 16:13:42”>
fred.save
=> true
fred.reload
=> #<User id: 1, name: “Fred”, created_at: “2009-06-24 16:13:42”,
updated_at: “2009-06-24 16:13:42”>
wilma = User.create(:name => “Wilma”)
wilma.save
wilma.reload
play = Like.create(:name => “playing with BamBam”)
play.save
eat = Like.create(:name => “eating Bronto burgers”)
eat.save
laugh = Like.create(:name => “laughing at Freds big shadow”)
laugh.save -
connect your browser to: http://localhost:3000/users/1.xml
connect your browser to: http://localhost:3000/users/2.xml
connect your browser to: http://localhost:3000/likes/1.xml
connect your browser to: http://localhost:3000/likes/2.xml
connect your browser to: http://localhost:3000/likes/3.xml
What could be simpler?
If you are using active resource from another machine over a network
and you have something like this:
require ‘rubygems’
require ‘activeresource’
ResourceServer = ‘http://mysite.com:3000’
class ChildRec < ActiveResource::Base
self.site = ResourceServer
#self.element_name = ‘’
end
class ParentRec < ActiveResource::Base
self.site = ResourceServer
#self.element_name = ‘’
end
Then it’s no problem to do stuff like:
par = ParentRec.find(31)
However, if ParentRec has many child_recs
I was saying basically that stuff like using: par.child_recs - I
was having difficulty getting to work at the time I had looked into
that a few months ago.
I can easily do
child = ChildRec.new()
child.parent_rec_id = par.id
Jedrin
Here is an old pdf from Ryan Daigle that talks about this. It appears to
be notes from a talk? I haven’t been able to get it to work so the
functionality discussed might have been removed from later versions of
Active Resource.
http://ryandaigle.com/assets/2007/3/14/REST_ARes.pdf
Jedrin wrote:
If you are using active resource from another machine over a network
and you have something like this:require ‘rubygems’
require ‘activeresource’ResourceServer = ‘http://mysite.com:3000’
class ChildRec < ActiveResource::Base
self.site = ResourceServer#self.element_name = ‘’
endclass ParentRec < ActiveResource::Base
self.site = ResourceServer#self.element_name = ‘’
endThen it’s no problem to do stuff like:
par = ParentRec.find(31)
However, if ParentRec has many child_recs
I was saying basically that stuff like using: par.child_recs - I
was having difficulty getting to work at the time I had looked into
that a few months ago.I can easily do
child = ChildRec.new()
child.parent_rec_id = par.id