# Using set\_primary\_key breaks acts\_as\_tree with non-integer c

**URL:** <https://www.ruby-forum.com/t/using-set-primary-key-breaks-acts-as-tree-with-non-integer-c/63848>\
**Category:** Rails\
**Created:** [June 19, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/using-set-primary-key-breaks-acts-as-tree-with-non-integer-c/63848 "2006-06-19T21:39:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 19, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/using-set-primary-key-breaks-acts-as-tree-with-non-integer-c/63848/1 "2006-06-19T21:39:48Z")

</div>

I just switched from using the standard “id” column into using my own  
primary key and generating my own unique id for each record. But, this  
breaks acts\_as\_tree.

Because my new primary key is not an integer, it breaks the SQL query  
as follows:

## StatementInvalid in PagesController#create

Mysql::Error: #42S22Unknown column ‘1\_3’ in ‘where clause’: SELECT \*  
FROM pages WHERE (parent\_id = 1\_3 AND account\_id = 1) ORDER BY  
position DESC LIMIT 1

Because parent\_id is also now a :string column, the 1\_3 should be put  
into single quotes like so:

SELECT \* FROM pages WHERE (parent\_id = ‘1\_3’ AND account\_id = 1)  
ORDER BY position DESC LIMIT 1

… but acts\_as\_tree assumes that the default parent\_id column should  
be an integer.

Any ideas on how I can get Rails to treat that parent\_id as a string  
instead of an integer?

Jeff

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 19, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/using-set-primary-key-breaks-acts-as-tree-with-non-integer-c/63848/2 "2006-06-19T21:50:23Z")

</div>

Okay, a quick update…

I narrowed it down to being where I set my Page’s parent\_id value…

This Is What I Have:  
page.parent\_id = params[:id]

Then, I tried this to force those single quotes:  
page.parent\_id = “’#{params[:id]}’”

and it inserts the record but includes the single quotes in the pages  
table so in the parent\_id column, the value is: ‘1\_3’ … but I don’t  
want the single quotes saved!

So, I tried:  
page.parent\_id = “#{params[:id]}”

…and that got me back to the original problem.

I also tried:  
page.parent\_id = “#{params[:id].to\_s}”

and the same thing…

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 19, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/using-set-primary-key-breaks-acts-as-tree-with-non-integer-c/63848/3 "2006-06-19T23:42:33Z")

</div>

Okay, it was all my fault. Not ActiveRecord’s!

I also had an acts\_as\_list with a scope defined. That scope thought  
that parent\_id was an integer.

acts\_as\_list :scope =\> ‘parent\_id = #{self.parent\_id} AND account\_id =  
#{self.account\_id}’

But, when I changed it to this:  
acts\_as\_list :scope =\> “parent\_id = ‘#{self.single\_quoted\_parent\_id}’  
AND account\_id = #{self.account\_id}”

… changing the single quotes to double quotes around the entire  
:scope value it still broke. It seems that the scope will only take  
the sql statement in single quotes. SO, a hack-around it, I created a  
method on the Model to add the single quotes:

acts\_as\_list :scope =\> ‘parent\_id = #{self.single\_quoted\_parent\_id}  
AND account\_id = #{self.account\_id}’

def single\_quoted\_parent\_id  
“’#{self.parent\_id}’”  
end

… it works, but seems messy. What am I missing in my ruby syntax to  
get the scope parameters to work without single quotes?

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 19, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/using-set-primary-key-breaks-acts-as-tree-with-non-integer-c/63848/4 "2006-06-19T23:42:42Z")

</div>

Um, that should read like this:

But, when I changed it to this:  
acts\_as\_list :scope =\> “parent\_id = ‘#{self.parent\_id}’  
AND account\_id = #{self.account\_id}”

… changing the single quotes to double quotes around the entire  
:scope value it still broke.
