Using a query as a class variable

Hi everyone, I have the following basic rules: Subject has many elements Element has many subjects Element has many knowledges which are either enabled or required Element has many skills which are either enabled or required

I have designed the schema to have 6 tables, Subjects, Elements, PriorKnowledge, PriorSkills, EnabledKnowledge and EnabledSkills.

Now to my problem. I am trying to build a page that will display all this information. It will display a subject and all its elements, and all of the elements prior and enabled, knowledges and skills.

The thought that I had was to use has_many :through relationships, but I don't know how to handle both prior and enabled cases.

My next thought was to use a sql query to select the knowledge and skill elements required and save them as class variables in elements. However I don't know how to get the ID of the element i am working with.

This is the SQL query that does the job:        select description from knowledges where id in (select knowledges_id from prior_knowledges where elements_id = 1);

I had this line in the element.rb file:        @@pk = Knowlegde.find_by_sql("select description from knowledges where id in (select knowledges_id from prior_knowledges where elements_id = " + id.to_s + ");") But i kept getting a empty array but executing the same command in SQL works, and executing the command manually in the console also works.

Is PriorKnowledge the join table between elements and EnabledKnowledges? If so has_many :through sounds like the right thing - what did you try that didn't work ?

Fred

Hi everyone, I have the following basic rules: Subject has many elements Element has many subjects Element has many knowledges which are either enabled or required Element has many skills which are either enabled or required

I have designed the schema to have 6 tables, Subjects, Elements, PriorKnowledge, PriorSkills, EnabledKnowledge and EnabledSkills.

You talked about required earlier, is that the same as prior?

Is there a good reason why you don't just put PriorKnowledge and EnabledKnowledge in the same class and table with a boolean to indicate which?

Now to my problem. I am trying to build a page that will display all this information. It will display a subject and all its elements, and all of the elements prior and enabled, knowledges and skills.

The thought that I had was to use has_many :through relationships, but I don't know how to handle both prior and enabled cases.

I am obviously missing something, why can't you just say @element.prior_knowledge and @element.enabled_logic

My next thought was to use a sql query to select the knowledge and skill elements required and save them as class variables in elements. However I don't know how to get the ID of the element i am working with.

This is the SQL query that does the job: select description from knowledges where id in (select knowledges_id from prior_knowledges where elements_id = 1);

Now you seem to have a 'knowledges' table which you have not mentioned before.

Colin

Yeah, Sorry, I also have a Knowledge and Skills table which i forgot to add to my initial post. And yes, prior is the same as required.

I think what Colin suggested about using a boolean may actually work quite well.

The problem i was having using the :through was that in writing, "has_many knowledges, :through prior_knowledges", i would also have, "has_many knowledges, :through enabled_knowledges". But using a boolean should solve that problem. Thanks very much for your suggestions, and sorry about my convoluted explanations.

Colin Law wrote: