Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 316787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:24:57+00:00 2026-05-12T08:24:57+00:00

After reading the book RESTful Web Services by Leonard Richardson and Sam Ruby, it

  • 0

After reading the book RESTful Web Services by Leonard Richardson and Sam Ruby, it seems to me, that the to_xml method of rails is not as restful as it should be. Specifically, the book introduces the Resource Oriented Architecture, one of the tenets of which is connectedness: Representations of a resource should contain not only the data of the resource, but also links to other resources.

However, when rails scaffolds a resource, it implements requests for a xml representation by deferring to [model]#to_xml. This method does not have access to the normal path helpers, so any links to other resources are only indicated by their ids, not by their uris.

I have solved this problem for now, but the solution doesn’t seem very robust: Given a Employer resource with nested Employees, the following code (sort of) adds uris to their xml serialization:

class Employee < ActiveRecord::Base

  include ActionController::UrlWriter

  belongs_to :employer

  def to_xml(options = {})
    options[:procs] = [ Proc.new {|options| options[:builder].tag!('uri', employer_employee_path(employer, self)) } ]
    if options[:depth].nil?
      options[:depth] = 1
    end
    if options[:depth] != 0
      options[:depth] -= 1;
      options[:include] = [:employer]
    end
    super(options)
  end
end

class Employer < ActiveRecord::Base

  include ActionController::UrlWriter

  has_many :employees

  def to_xml(options = {})
    options[:procs] = [ Proc.new {|options| options[:builder].tag!('uri', employer_path(self)) } ]
    if options[:depth].nil?
      options[:depth] = 1
    end
    if options[:depth] != 0
      options[:depth] -= 1;
      options[:include] = [:employees]
    end
    super(options)
  end
end

The UrlWriter allows me to properly create the path of a resource (not the full uri, though. The domain will have to be pasted on to the path by the clients of my web service). Now the models are responsible for their own uri, and for including the representation of any connected resource. I use the :depth option to avoid recursing endlessly.

This method works, but as stated before, it doesn’t seem quite right, with all the duplication. Has anybody else had the same problem and does anybody else have a better idea of how to get uris in the xml representation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:24:57+00:00Added an answer on May 12, 2026 at 8:24 am

    You could use the :methods option to include additional values. For example.

    # in controller
    employer.to_xml(:include => :employees, :methods => [:uri])
    
    class Employee < ActiveRecord::Base
      include ActionController::UrlWriter
      belongs_to :employer
    
      def uri(options = {})
        polymorphic_path([employer, self])
      end
    end
    
    class Employer < ActiveRecord::Base
      include ActionController::UrlWriter
      has_many :employees
    
      def uri(options = {})
        polymorphic_path(self)
      end
    end
    

    Notice I’m using polymorphic_path here. This is a more generic path method which you might find more abstractions through.

    However, I do not think this is that great of a solution. Including UrlWriter in the model is messy and can take a long time if you have a large number of routes. An alternative is to make the “uri” a simple accessor method.

    # in model
    attr_accessor :uri
    

    And then you can set the uri for the model(s) in the controller. Unfortunately that is pretty messy too.

    employer.uri = polymorphic_path(employer)
    employer.employees.each { |e| e.uri = polymorphic_path([employer, e]) }
    employer.to_xml(:include => :employees, :methods => [:uri])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Categories extend the original class, but they don't subclass it,… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer Haven't tested this, but it's something like: RewriteRule \.php$ -… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer "0:0:0:0:0:0:0:1" is the IPv6 loopback address as defined in RFC… May 12, 2026 at 12:54 pm

Related Questions

I'm new to Windows programming and after reading the Petzold book I wonder: is
After reading the Head First Design Patterns book and using a number of other
I am currently reading the book called "Programming ActionScript 3.0" available from Adobe. After
I've been reading the book Enterprise Rails by Dan Chak and it got me
I just purchased C++ GUI Programming with Qt4 and after reading the code samples

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.