Topic: Modelling confusion: pages, chunks and artifacts

Hello, and thanks.

I'm looking for feedback on this:

I have Pages - each Page has some Chunks - each Chunk is made up of a series of Artifacts and these Artifacts are asigned to either the top_panel, middle_panel or bottom_panel of the Chunk.

I could create Top_Panel... objects or I guess I could create a Panel superclass... I'm trying to think less like a "java programmer";  how would a "rails programmer" do it? ;->

thanks, again
sporb

Re: Modelling confusion: pages, chunks and artifacts

Does a top panel behave differently than a bottom panel besides its location? If so, you may want to make them separate classes, but if not, I recommend adding top/middle/bottom_artifact_id columns in the chunks table and doing this:

class Page < ActiveRecord::Base
  has_many :chunks
end

class Chunk < ActiveRecord::Base
  belongs_to :page
  belongs_to :top_artifact, :foreign_key => 'top_artifact_id', :class_name => 'Artifact'
  belongs_to :middle_artifact, :foreign_key => 'middle_artifact_id', :class_name => 'Artifact'
  belongs_to :bottom_artifact, :foreign_key => 'bottom_artifact_id', :class_name => 'Artifact'
end

class Artifact < ActiveRecord::Base
end


Another way to do it is put chunk_id and position columns in the artifacts table. Then a Chunk simply has_many :artifacts and you use the position column to determine where to place it.

Railscasts - Free Ruby on Rails Screencasts

Re: Modelling confusion: pages, chunks and artifacts

Thanks very much! Sometimes, one can go a little "class crazy" ;-> the three foreign keys work nicely.

Now, please: How will I access the top_artifact.name in the view, when displaying a given chunk?
I've tried several approaches; it tells me that chunk.top_artifact.name is asking for a nil.name  for example ...


(understanding more each day!)
sporb

Re: Modelling confusion: pages, chunks and artifacts

The chunk.top_artifact is returning nil for some reason. Sounds like a good time to fire up script/console and play around with the model. It should return the Artifact with the id in the top_artifact_id column.

If you are looping through several chunks, some may be missing the top artifact which could be causing the error. You can add a condition to prevent this.

<%= chunk.top_artifact.name unless chunk.top_artifact.nil? %>

Railscasts - Free Ruby on Rails Screencasts

Re: Modelling confusion: pages, chunks and artifacts

Yes, indeed; the console shows a value for the name of the top artifact when I try this:

c = Chunk.find(1)
c.top_artifact.name

... so the object relationship is there after all.

I'll implement a check for nils, as you have suggested.

Thanks for all your help!

-sporb