Demetrius
2009-Nov-09  14:39 UTC
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!!!
Demetrius
2009-Nov-12  12:54 UTC
Re: aggregation: use composed_of with multiple classes (tables)
Is anyone able to share some insight regarding composed_of pulling from two tables? On Nov 9, 6:39 am, Demetrius <demetri...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> Does anyone know how to usecomposed_ofto pull data from multiple > classes (tables)? I know how to usecomposed_ofto 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!!!