views:

109

answers:

2

Hi,

I have 2 models 'item' and 'activity'. An 'item' can have many 'activities' (limited number of activities, used by all items). To make it clearer: this is represented by a table with one item per row, one activity per column and something/nothing on the intersection if a line exists in the table matrices.

to see an image (worth a 1000 words :)): http://tinypic.com/r/33ygtj9/3

The relationships are (i think) set correctly: item has_many matrices, item has_many activities through matrices

activity has_many matrices, activity has_many items through matrices

matrix belongs to item, matrix belongs to activity

Some additional/useful info: - the matrix is represented graphically by a table with one item per row, one activity per column and OK/KO on the intersection if a line exists in the table matrices. - i have 2 other models in the picture: a course model and a chapter model (course has many chapters, chapter has many items) - Everything happens in the Course 'show' view

So far, i've managed to directly create a line (without going through a form) in the matrices table with the activity_id and the item_id. However I struggle to delete a line, because I don't understand how to access the matrices.id value.

The only way i've found yet is, for each item, to loop through all available activities for this item in a hash (key = activity_id, value = matrices_id). If the activity is not available, i display OK + a link to create a line in matrices else,OK + link to destroy.

Is there an easier/more 'Rails' way to do that ? i.e. get rid of the hash?

thanks for your help.

Pierre

A: 

So:

class Item
  has_many :activities, :through => :matrix
end

class Activity
  has_many :items, :though => matrix
end

class Matrix
   belongs_to :item
   belongs_to :activity
end

Firstly, I assume you are recording more information in the matrix, otherwise you might have used has_and_belongs_to_many relationships between items and activities.

You could add has_many relationships to make thing easier:

class Item
  has_many :activities, :through => :matrix
  has_many :matrices
end

class Activity
  has_many :items, :though => matrix
  has_many :matrices
end

class Matrix
   belongs_to :item
   belongs_to :activity
end

I am not sure how the Course and Chapter models are linked, so it's hard to say anything about them.

The view might be something like:

@chapter.items.include?(course)? link_to("Destroy", link_to_destroy) : link_to("Create", create_a_line)
askegg
the relationships already look like what you've proposed.regarding chapter and course:course has_many chapters (course_id)chapter belongs_to coursethe last part (include) is actually not what i'd like to do.I'd like something likeitem.matrices[0].idbut even though item.matrices.size returns something, i cannot access a specific index
Pierre
i'm starting to go crazy.if i do<%item.matrices.each do |mt|%><%=mt.id%><%end%>i can see each matrix id but item.matrices[0].id won't work!Am i stupid?
Pierre
A: 

OK, I am stupid...

i was focusing on the lines where i know that item.matrices[x] was not nil... and the error came from the lines where items.matrices[x] is nil...

So everything is solved by just adding a unless items.matrices[x].nil?

That's the fun thing about programming: wasting time for things like this... but learning anyway :)

Pierre