# I need superman!

**URL:** <https://www.ruby-forum.com/t/i-need-superman/52776>\
**Category:** Rails\
**Created:** [January 19, 2006, 12:52am UTC](https://www.ruby-forum.com/t/i-need-superman/52776 "2006-01-19T00:52:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 19, 2006, 12:52am UTC](https://www.ruby-forum.com/t/i-need-superman/52776/1 "2006-01-19T00:52:50Z")

</div>

I have a legacy database that I can’t figure out. I’ve tried hundres of  
times and spent way tooooo much time trying to figure this out. I don’t  
think that rails can do it. Maybe there is a superman here who can get  
this too work. I’m going to post my sql dump to produce a very simple  
database. If anyone can get rails to work with this THEY WILL BE MY  
HERO!!.

This is the statment I would normally use to create the join.  
“select \* from parent,child where parent.number = ‘2005121212121200’ and  
CONCAT(SUBSTRING(child.item\_number,1,14),‘00’) = parent.number”

As you can see child.item\_number is links to the parent with  
CONCAT(SUBSTRING(child.item\_number,1,14),‘00’) = parent.number. The  
last two digits of parent.number are incremented for each  
child.item\_number. To top it off, child has no primary because  
child.item\_number can have duplicates. This is a simplistic example of  
an actuall database I am trying to use rails with. Are you superman? I  
really hope that rails can handle this database. I would much rather use  
rails than write out thousands of lines of perl and sql to handle this.  
Someone please tell me this is possible!

Here is the sql to create the database with some simple information

CREATE TABLE `child` (  
`item_number` varchar(16) NOT NULL default ‘’,  
`first_name` varchar(8) NOT NULL default ‘’,  
`last_name` varchar(8) NOT NULL default ‘’  
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

## – – Dumping data for table `child`

INSERT INTO `child` VALUES (‘2005121212121201’, ‘charlie’, ‘bowman’);

* * *

## – – Table structure for table `parent`

CREATE TABLE `parent` (  
`order_number` varchar(16) NOT NULL default ‘’,  
`client_id` varchar(16) NOT NULL default ‘’,  
`submitted_by` varchar(8) NOT NULL default ‘’,  
PRIMARY KEY (`order_number`)  
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

## – – Dumping data for table `parent`

INSERT INTO `parent` VALUES (‘2005121212121200’, ‘45645465’, ‘smith’);

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 19, 2006, 1:58pm UTC](https://www.ruby-forum.com/t/i-need-superman/52776/2 "2006-01-19T13:58:42Z")

</div>

Hi, charlie,

what a unique occasion for clark kenting 😉

What YOU already did works for me, you really ought to check what is  
wrong with your rails and/or mysql configuration, try another machine  
and save yourself some hair…

My development.log:

Processing ParentController#index (for 127.0.0.1 at 2006-01-19 13:12:06)  
[GET]  
Parameters: {“action”=\>“index”, “controller”=\>“parent”}  
e[4;36;1mParent Count (0.000000)e[0m e[0;1mSELECT COUNT(_) FROM  
parent e[0m  
e[4;35;1mParent Load (0.000000)e[0m e[0mSELECT \* FROM parent LIMIT  
0, 10e[0m  
Rendering layoutfalseactionlist within layouts/parent  
Rendering parent/list  
e[4;36;1mParent Columns (0.000000)e[0m e[0;1mSHOW FIELDS FROM  
parente[0m  
e[4;35;1mChild Load (0.016000)e[0m e[0mselect child._ from  
child,parent where CONCAT(SUBSTRING(child.item\_number,1,14),‘00’) =  
parent.order\_numbere[0m  
e[4;36;1mChild Columns (0.000000)e[0m e[0;1mSHOW FIELDS FROM  
childe[0m  
Completed in 0.06300 (15 reqs/sec) | Rendering: 0.03100 (49%) | DB:  
0.03200 (50%) | 200 OK [[http://localhost/parent](http://localhost/parent)]

the database.yml:

development:  
adapter: mysql  
database: testit\_development  
username: root  
password: xxx  
#socket: /path/to/your/mysql.sock

# Connect on a TCP socket. If omitted, the adapter will connect on

the

# domain socket given by socket instead.

host: localhost  
port: 3306  
encoding: utf8

environment.rb:

# Include your application configuration below

ActiveRecord::Base.pluralize\_table\_names = false

The parent:

class Parent \< ActiveRecord::Base

```
    set_primary_key :order_number
    has_many :child,
                    :finder_sql => "select child.* from

```

child,parent " +  
“where CONCAT(SUBSTRING(child.item\_number,1,14),‘00’) =  
parent.order\_number”

end

the child:

class Child \< ActiveRecord::Base

set\_primary\_key :item\_number  
belongs\_to :parent,  
:foreign\_key =\> ‘order\_number’

end

the view:

# Listing parents

\<% for parent in @parents -%\>  
\<%= parent.order\_number %\>  
  
\<%= parent.client\_id %\>  
  
\<%= parent.submitted\_by %\>  
  
**Orders in detail**  
  
\<% for child in parent.child -%\>  
\<%= child.item\_number %\>  
  
\<%= child.first\_name %\> \<%= child.last\_name %\>  
  
\<% end -%\>  
\<% end -%\>

\<%= link\_to ‘Previous page’, { :page =\> @parent\_pages.current.previous }  
if @parent\_pages.current.previous %\>  
\<%= link\_to ‘Next page’, { :page =\> @parent\_pages.current.next } if  
@parent\_pages.current.next %\>

  

\<%= link\_to ‘New parent’, :action =\> ‘new’ %\>

the output:

Listing parents

2005121212121200  
45645465  
smith  
_Orders in detail_  
2005121212121201  
charlie bowman

New parent [http://localhost:3002/parent/new](http://localhost:3002/parent/new)

Best regards  
Jan

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 25, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/i-need-superman/52776/3 "2006-01-25T23:35:53Z")

</div>

I guess I can be my own superman! I finall got this stuff working. It  
turns out it was a combination of bad code and bad c mysql binding on  
fedora core 4. I uninstalled mysql (gem uninstall mysql) and my  
segmentation faults ended. Here is the parent and child models. I hope  
this helps someone else!

class Parent \< ActiveRecord::Base  
set\_primary\_key :order\_number  
has\_many :child,  
:finder\_sql =\> ‘select \* from child where  
CONCAT(SUBSTRING(child.item\_number,1,14),‘00’) = #{order\_number}’  
end

class Child \< ActiveRecord::Base  
belongs\_to :parent,  
end

In my enviroment.rb  
ActiveRecord::Base::pluralize\_table\_names = false

Thanks to everyone who helped!
