Monday, November 9, 2009

[Rails] aggregation: use composed_of with multiple classes (tables)

Does anyone know how to use composed_of to pull data from multiple
classes (tables)? I know how to use composed_of to aggregate columns
within the same table but am running in to trouble pulling from two
tables.

The example below works but I don't want to see standard.strand_id, I
want to see strand.name (which is in the Strand table, not the
Standard table).

class Standard < ActiveRecord::Base
composed_of :full_standard,
:mapping =>
[
%w[grade grade],
%w[strand_id strand_id]
%w[number number]
]

class FullStandard < ActiveRecord::Base
attr_reader :grade, :strand_id, :number

def initialize(grade, strand, number)
@grade = grade
@strand_id = strand_id
@number = number
end

def to_s
[ @grade, @strand_id, @number ].compact.join(" ")
end
end

Again, this works, BUT I don't want to see the strand_id, I want to
see strand.name (a column in the Strand table--not the Standard table)

Also, here are the table relationships:

class Standard
belongs_to :strand

class Strand
has_many :standards

Thank you!!!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate