2

My setup: Rails 2.3.10, Ruby 1.8.7

Here are my models

class User
 has_many :user_projects
end

class Project
 has_many :user_projects
 #has a column named "project_type"
end

class UserProject
 belongs_to :user
 belongs_to :project
 #fields: project_id, user_id
end

When I return a JSON string of a user and his related user_projects records, I also want to include in the user_project record the project.project_type column. Note: I don't want to also include the entire project record in the results. A possible solution is dup the project_type field in user_projects but I prefer not to do that if possible, is there another way to accomplish this during the find/read action?

Just to be clear, here's the JSON output I'm looking for

{
  "user": {
    "username": "bob",
    "id": 1,
    "email": "[email protected]"
    "user_projects": [
      {
            "id": 15,
            "user_id": 1,
            "project_id": 10,
            "project_type": "marketing"
      }
      {
            "id": 22,
            "user_id": 1,
            "project_id": 11,
            "project_type": "sales"
      }
     ]
}

2 Answers 2

2
class UserProject
   belongs_to :user
   belongs_to :project
   #fields: project_id, user_id
   attr_reader :type


  def type
    self.project.type
  end
 end

 class MyController < AC

   def action
     @model = whatever
     respond_to do |format|
       format.json { render :json => @model.to_json(:methods => :type)}
     end

   end
 end

Hope this helps.

5
  • Thanks, this is closer to what I was thinking of doing except that I need to return all the user details and include user_projects plus the type column as part of the user_project details, any idea on syntax?
    – Bob
    Jan 22, 2011 at 0:45
  • Never mind, I figured it out, that worked exactly like I hoped, thanks!
    – Bob
    Jan 22, 2011 at 0:50
  • One last note, it needs to be def self.type
    – Bob
    Jan 22, 2011 at 1:19
  • Oops, I spoke too soon, it is show a value of null, so it isn't working
    – Bob
    Jan 22, 2011 at 2:14
  • Pasta: the attr_reader line shouldn't be there. Bob: you don't want def self.type, that would make it a class method
    – mckeed
    Feb 1, 2011 at 17:57
2

You could try using the :only key in a nested include:

user.to_json(:include => {:user_projects => {:include => {:project => {:only => :type}}}})

But I would add has_many :projects, :through => :user_projects to User so you can do the simpler:

user.to_json(:include => {:projects => {:only => [:id, :type]}})

Also, an off-topic cautionary note: never use 'type' as a column name in Rails unless you are using STI (ie. the project types are ruby subclasses of Project).

-

Edit

Here's a way to add project_type to UserProject like you want

class UserProject
  belongs_to :user
  belongs_to :project
  delegate :type, :to => :project, :prefix => true
end

user.to_json(:include => {:user_projects => {:methods => :project_type}})
2
  • Good point on not using 'type' as a column, I'm actually not doing that, bad example on my part. I prefer to only return user_projects.user_id, project_id, and project.type columns all in the context within the user_project scope, is there a way to do that? I thought I might be able to use attr_accessor and somehow pull the value from project when a user_project record is being accessed but can't get that to work.
    – Bob
    Jan 22, 2011 at 0:29
  • look my answer, seems it's what you want to achieve
    – Pasta
    Jan 22, 2011 at 0:35

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.