0

I have a User model in rails with the following method:

  def current_project
    if(self.active_project != nil)
      @project = Project.find(self.active_project)
    end
  end

Note: active_project is an integer field on User which corresponds to the id of the project they are working on.

When I get the xml version of the User model, the project only renders the id, not all the other fields on that model - title, description, etc.

What do I need to do get the xml version of User to render all of the properties of the Project?

3
  • There seems to be no difference between active_project and current_project, so I don't think you need this method. Aside from that I don't have a solution for your problem. I tried it in one of my projects and when I do the equivalent of self.active_project.to_xml, it works fine.
    – Mischa
    Jul 4, 2011 at 1:19
  • sorry, active_project is just an id, the id of the project the person is working on
    – brendan
    Jul 4, 2011 at 1:20
  • Ah, I misread your question. You mean that when you call user_instance.to_xml you only get the id of the project, instead of all the fields?
    – Mischa
    Jul 4, 2011 at 1:28

1 Answer 1

1

I don't get the use of having such a current_project method. Maybe you can give some more information? Right now it seems that a simple

format.xml { render :xml => @user.to_xml(:include => :active_project) }

should work for your purpose. Am I missing something?

Edit: After reading the comments to the original thread

format.xml { render :xml => @user.to_xml(:methods => :current_project) }

will do the job.

3
  • Yes, this seems to be the way to go.
    – Mischa
    Jul 4, 2011 at 1:34
  • The second line is actually what I am doing that's causing it to render just the id.
    – brendan
    Jul 4, 2011 at 2:05
  • Figured it out, had to set up a proper belongs_to/has_many relationship btwn Users and Projects, then the include in your first line of code worked. Thanks!!
    – brendan
    Jul 4, 2011 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.