In Rails 3.2.1
I have:
class Project < ActiveRecord::Base
attr_accessible :name, :description
has_many :subprojects
end
class SubProject < ActiveRecord::Base
attr_accessible :id_name, :description, :num_alloc, :project_id
belongs_to :projects
end
How can i return in a rails controller, an object that contain the “name” attribute (from the Project model) and the id_name, description and num_alloc (from the SubProject model).
In the controller, if i make
@results= SubProject.joins('LEFT OUTER JOIN.......)
@results contains only the attribute of the SubProject class because SubProject.joins(...) returns an SubProject object right?
So how can i return an object with attribute from the two model?
I would start at your model definitions: Your
has_manyassociation needs to have an underscore in the name –SubProject -> sub_project:Next, your
belongs_toneeds to be in a singular form:After you make these changes, you can query: