Polymorphic Associations... confusing. Do I need them?

Seems like what would work is a habtm relationships (and related
tables) between your Tutorial, and your Example and Question models.
That way, an example can belong to different tutorials, and a tutorial
can have multiple examples. (And the same with questions.)

You could define a Section model which belongs_to your Tutorial model and has a position attribute so you can determine the order.

In your Tutorial model
has_many :sections , :order => ‘sections.position ASC’

You then set up your Example and Question models so that they have a polymorphic has_many association with the Section model.

In Section:
belongs_to :sectionable , :polymorphic => true

In Question or Example:

has_many :sections , :as => :sectionable

Now you can say:

@tutorial.sections.each do |section|
  section=section.sectionable
  case section
  when Example
    puts section.some_example_method

  when Question
    puts section.some_question_method
  else
    puts 'what!'
  end
end

Has the nice benefit that you can recycle your examples and questions in next year’s tutorial if you’re lazy :slight_smile:

Daniel Bush wrote:

  has_many :examples, :as => :sections
And get:
sections for each tutorial, also, to make sure they appear in the right
order.

Does that make sense? I think single table inheritance might do this,
but I'm planning on using examples and questions for a bunch of other
things, so I don't think it would work that well. I'm thinking about
defining a whole new "section" model, and using that, but I don't know
yet.

I must be missing something here, but given that you have separate
models [Tutorial, Question, Example] then what's wrong with:

Tutorial << ActiveRecord::Base
  has_many :questions
  has_many :example
end

Question << << ActiveRecord::Base
  belongs_to :tutoiral
end

Example << ActiveRecord::Base
  belongs_to :tutorial
end

Then given that Ruby is a duck typed language just put the questions and
examples together in one array and as long as you stick to the common
API for your ToC, or whatever it will just work:

toc = tutorial.questions.concat(tutorial.examples)

toc.each do |item|
  puts item.class
  puts item.name
end

Robert Walker wrote:

Tutorial << ActiveRecord::Base
  has_many :questions
  has_many :example
end

Oops, sorry for the typo. Should be:
  has_many :examples

I might be missing something too and over-complicating things.
The Section model in my previous post has a position attribute and allows Chris to create a tutorial of ordered sections where each section is linked to an Example or Question.

I’m not sure how you guarantee that with your version, although maybe with some effort you could.
Because the Examples and Questions are has_many Sections, they are also reusable in a future tutorial; it is the sections that are tied specifically to a given tutorial instance.

Anyway, it seemed like the Section abstraction was a nice way to show off polymorphic routes.

oh crap, I mean “polymorphic associations”.

The word polymorphic is getting too polymorphic…

Not sure how experienced you are with polymorphic assocs. I omitted some details chief of which is that you need two fields in your sections table: sectionable_id and sectionable_type. There’s a shortcut for doing this in a rails migration
t.references :sectionable , :polymorphic => true
or something like that.

Yeah, I was able to figure it out and everything's working great, just
like I wanted. Thanks again!

Daniel Bush wrote: