Gijis
when you specify has_many and belogs_to in your models rails creates
some nice ways to access your data. test this out and see if it helps:
instead of this
<% for question in @questions %>
<% if subject.id == question.subject_id %>
<% question.id %>
<br/>
<% end %>
<% end %>
Try
<% for question in subject.questions %>
<%= question.id %>
<br/>
<% end %>
Once you get that going you can use the same login to do categories
let me know if it works. i had the same problem a while ago
Sam
On 10/9/05, Gijs Nijholt
<gijs.nijholt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hello Rails users,
>
> I''ve just started using both Ruby and Rails, and I''m also
new to the
> MVC concept. (I''ve ordered the ''Agile...'' book
though).
>
> My database looks like:
>
> questions
> - id
> - question
> - subject_id
>
> subjects
> - id
> - subject
> - subject_id
>
> categories
> - id
> - category
>
> Now I want to make a list as follows:
> --------
> CATEGORY A
> SUBJECT A
> question 1
> question 2
> SUBJECT B
> question 3
> question 4
> CATEGORY B
> SUBJECT C
> question 5
> --------
> ...and so on...
> (it''s just simply listing the questions for each subject for each
category)
>
> In my app/models/questionaire.rb, I have made three classes in which I
> map the relations (or at least I think it does):
> --------
> class Category < ActiveRecord::Base
> has_many :subjects
> end
> class Subject < ActiveRecord::Base
> has_many :questions
> belongs_to: category
> end
> class Question < ActiveRecord::Base
> belongs_to :subject
> end
> --------
> in questionaire_controller.rb, I have a method list, where I pull all
> the questions and subjects (I skipped the categories for a moment):
> @subjects = Subjects.find(:all)
> @questions = Questions.find(:all)
>
> and in app/views/questionaire/list.rhtml, I have the following:
>
> <html>
> <head>
> <title>list</title>
> </head>
> <body>
>
> <h2>All subjects</h2>
> <% for subject in @subjects %>
> <h3><%= subject.subject %> (id #<%= subject.id
%>)</h3>
> Questions for this subject:<br/>
> <% for question in @questions %>
> <% if subject.id == question.subject_id %>
> <% question.id %>
> <br/>
> <% end %>
> <% end %>
> <br/>
> <% end %>
>
> </body>
> </html>
> --------------
> I''ve tried stuff for hours, and have read many wiki pages about
this,
> but I probably don''t get it..
> Can anyone help me out with this seemingly simple problem?
>
> Thanks in advance.
>
> Gijs Nijholt
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>