# Using a query as a class variable

**URL:** <https://www.ruby-forum.com/t/using-a-query-as-a-class-variable/191471>\
**Category:** Rails\
**Created:** [July 13, 2010, 7:03am UTC](https://www.ruby-forum.com/t/using-a-query-as-a-class-variable/191471 "2010-07-13T07:03:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zatta105](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ce7236/32.png) [@zatta105](https://www.ruby-forum.com/u/zatta105)\
**Post date:** [July 13, 2010, 7:03am UTC](https://www.ruby-forum.com/t/using-a-query-as-a-class-variable/191471/1 "2010-07-13T07:03:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zatta105](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ce7236/32.png) [@zatta105](https://www.ruby-forum.com/u/zatta105)\
**Post date:** [July 13, 2010, 10:13am UTC](https://www.ruby-forum.com/t/using-a-query-as-a-class-variable/191471/2 "2010-07-13T10:13:42Z")

</div>

On Jul 13, 6:03 am, Adrian Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

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

---

<div class="post-metadata">

**Author:** ![zatta105](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ce7236/32.png) [@zatta105](https://www.ruby-forum.com/u/zatta105)\
**Post date:** [July 13, 2010, 10:51am UTC](https://www.ruby-forum.com/t/using-a-query-as-a-class-variable/191471/3 "2010-07-13T10:51:11Z")

</div>

On 13 July 2010 06:03, Adrian Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![zatta105](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ce7236/32.png) [@zatta105](https://www.ruby-forum.com/u/zatta105)\
**Post date:** [July 13, 2010, 3:17pm UTC](https://www.ruby-forum.com/t/using-a-query-as-a-class-variable/191471/4 "2010-07-13T15:17:05Z")

</div>

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 L. wrote:

> On 13 July 2010 06:03, Adrian Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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
