AR: How to store and restore in YML


#1

Hello-

I have a database with entries that are segmented by customers. I would
like to be able to store / delete / restore a particular customer’s
information. I will need to rebuild the rows on restore and also
rebuild the associations for the different tables.

For example,
customer {
id
name
}
companies {
id
customer_id (belongs_to)
name
}
contacts {
id
company_id (belongs_to)
name
}

So, if I just write out YML for the rows in this database, I’d end up
with something like:
Customer: id=34, name=“Bob”
Company: id=134, customer_id=34, name=“Intel”
Company: id=149, customer_id=34, name=“Apple”
Contact: id=9383, company_id=134, name=“Jerry”
Contact: id=8392, company_id=149, name=“Robert”

The problem with this is that it is more difficult to regenerate those
rows because the associations would need to be broken and rebuilt.

What I would like to do is use the names in the YML output so I can
create records in a valid order, and do “finds” to find the right
records for associations. (Uniqueness is enforced so a find will find a
single record)

Customer: id=34, name=“Bob”
Company: id=134, customer_id=“Bob”, name=“Intel”
Company: id=149, customer_id=“Bob”, name=“Apple”
Contact: id=9383, company_id=“Intel”, name=“Jerry”
Contact: id=8392, company_id=“Apple”, name=“Robert”

I am interested in hearing any rails/ruby tricks that would help me do
this for a number of different tables in the database.

Any suggestions?

Jake


#2

Greetings!

I’m new to Ruby and Rails and am working through the Curt H. article.
I
created the model and controller per the article but I’m getting an
error
message (title: SystemStackError in Recipe#new; message: stack level too
deep) when I point my browser to http://127.0.0.1:3000/recipe/new

Any ideas what I’m doing wrong? Any help is very much appreciated!

Best regards,
Bill


#3

On Jan 1, 2006, at 5:20 PM, Jake J. wrote:

id
name

Company: id=134, customer_id=“Bob”, name=“Intel”
Company: id=149, customer_id=“Bob”, name=“Apple”
Contact: id=9383, company_id=“Intel”, name=“Jerry”
Contact: id=8392, company_id=“Apple”, name=“Robert”

I am interested in hearing any rails/ruby tricks that would help me do
this for a number of different tables in the database.

Any suggestions?

Have you taken a look at the way ActiveRecord does ordered fixtures?
It uses YAML::Omap to ensure that the hashes are each inserted in
their proper order.

http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML/Omap.html

Duane J.
(canadaduane)
http://blog.inquirylabs.com/


#4

ulimit -s 8192

before starting the webrick


#5

Duane J. wrote:

Have you taken a look at the way ActiveRecord does ordered fixtures?
It uses YAML::Omap to ensure that the hashes are each inserted in
their proper order.

http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML/Omap.html

Duane J.
(canadaduane)
http://blog.inquirylabs.com/

Hi Duane-

Can you explain this a bit more? I’m not sure how these ordered
fixtures fit into my question.

My primary concern isn’t so much the order as it is recreating records
properly with new association IDs. I can’t create records with the same
ID because they may be added to a different database that may already
have those IDs.

Jake


#6

Hi Ian,

That looks like a unix/linux command line entry. I’m working on Win2K.
Any
idea how to accomplish the equivalent on that platform?

Thanks!
Bill

----- Original Message -----
From: “Ian H.” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Sunday, January 01, 2006 11:08 PM
Subject: Re: [Rails] Newbie problem with RoR - stack level too deep

ulimit -s 8192

before starting the webrick

On 1/2/06, Bill W. removed_email_address@domain.invalid wrote:

Greetings!

I’m new to Ruby and Rails and am working through the Curt H. article.
I
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#7

It may be a sign of an indefinite recursive loops


#8

ulimit -s 8192

before starting the webrick

I’m not familiar with the Curt H. article mentioned, but is there
supposed to be a deeply recursive function? I’m guessing you’re seeing
the SystemStackError because there may be some unintended infinite
recursion in your code.

That is, for whatever reason, it sounds like your Recipe#new may be
getting into an infinite loop, recursively.

I’m a Rails newbie myself so I don’t know what to suggest other than
looking at the backtrace from the SystemStackError exception: it will
show who-called-whom-called-whom-called-whom… and might illuminate
the problem.

Hope this helps,

Bill


#9

I don’t think the stack problem I’m having is a result of code I wrote.
That’s because I haven’t written any code, per se :wink: My current
hypothesis
is that I either have something wrong installed, or something installed
wrong.

My current config (as reported by ‘ruby -v’, ‘gem -v’, ‘rails -v’,
‘mysql
\s’ and ‘ruby script\server’) is:
Ruby: 1.8.2 (2004-12-25) [i386-mswin32]
gem: 0.8.3
Rails: 0.14.3
MySQL: 4.1.16-nt
WEBrick: 1.3.1

The article I referred to is “Rolling with Ruby on Rails.” It’s at:
http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html

WRT background info, I’m working through it on a fresh Win2K
installation.
The RoR install went fairly well. The only significant problem I had
was
with the Rails install. Apparently the gems package that ships with the
Windows installer is out of date and I had to bring it up to date with a
“gems install rails -v 0.14.3”. That got me to a “Rails installed
successfully” message.

The only config issue that jumped out at me was that the article
instructed me to ‘Download the latest “essential” version of of the
MySQL
Windows installer (Currently, that is Windows Essentials (x86) 4.1.7)’.
ALL
the versions available for download at
dev.mysql.com/downloads/mysql/4.1.html are 4.1.16, not 4.1.7. Initially
I
thought that the version info at mysql.com was a typo. Now I’m not so
sure.

The article took me through using Rails to generate a simple controller
to
display text on a web page just fine. The problem showed up with the
use of
the database.

I set up the database, table, and fields per the article with no problem
(There was one surprise in that the article included an “important note”
to
the effect that MySQL would name the primary key ‘Id’ and I’d need to
change
it to ‘id’ to make Rails happy. In fact, MySQL named it ‘id’ so I
didn’t
need to change it.)

I used ‘ruby script\generate model Recipe’ and ‘ruby script\generate
controller Recipe’ and the expected files (per the article) were
generated.
Pointing my browser at ‘http://127.0.0.1:3000/recipe/new’ resulted in a
page
being displayed with the message
“Unknown action
no action responded to new”
That’s pretty much what I expected.

The stack problem happened when I added the ‘scaffold :recipe’ line to
‘recipe_controller.rb’ Pointing my browser to the same URL now
generates
the error page. It looks like somewhere over 1000 identical lines of
“c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/callba
cks.rb:236:in `initialize_without_callbacks’”. That section is followed
by
a 40-50 calls that include webrick. The last line of the error message
says
“script/server:3” I can send along the page itself with all the calls
listed if that’s of any help.

Does any of this help? I’m pretty impressed with RoR based on the
article.
Now if I could only get it to WORK ;-(

Thanks in advance to anyone who can help.

Best regards,
Bill

----- Original Message -----
From: “Bill K.” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Monday, January 02, 2006 5:44 AM
Subject: Re: [Rails] Newbie problem with RoR - stack level too deep

On 1/2/06, Bill W. removed_email_address@domain.invalid wrote:

I’m new to Ruby and Rails and am working through the Curt H.
article.

I created the model and controller per the article but I’m getting an
error

message (title: SystemStackError in Recipe#new; message: stack level
too


#10

From: “Bill W.” removed_email_address@domain.invalid

activesupport-1.2.3
fxruby-1.2.2-mswin32
fxruby-1.4.3-mswin32
rails_analyzer_tools-1.1.0
rails-0.14.3
rake-0.6.2
sources-0.0.1

I found this via: gem list --remote

mysql (2.7, 2.6, 2.5.1)
MySQL/Ruby provides the same functions for Ruby programs that the
MySQL C API provides for C programs.

Hope this helps,

Bill


#11

Is there supposed to be a MySQL gem installed for the example in the RoR
article to work? If so, how / where do I get it? I previously did a
‘gem
update’.

The files in c:\ruby\lib\ruby\gems\1.8\gems\ are:
actionmailer-1.1.3
actionpack-1.11.0
actionwebservice-0.9.3
activerecord-1.13.0
activesupport-1.2.3
fxruby-1.2.2-mswin32
fxruby-1.4.3-mswin32
rails_analyzer_tools-1.1.0
rails-0.14.3
rake-0.6.2
sources-0.0.1

Thanks,
Bill

----- Original Message -----
From: “Bill W.” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Monday, January 02, 2006 11:01 AM
Subject: Re: [Rails] Newbie problem with RoR - stack level too deep

I don’t think the stack problem I’m having is a result of code I wrote.
That’s because I haven’t written any code, per se :wink: My current
hypothesis

The only config issue that jumped out at me was that the article
instructed me to ‘Download the latest “essential” version of of the MySQL
Windows installer (Currently, that is Windows Essentials (x86) 4.1.7)’.
ALL
the versions available for download at
dev.mysql.com/downloads/mysql/4.1.html are 4.1.16, not 4.1.7. Initially I
thought that the version info at mysql.com was a typo. Now I’m not so
sure.

The article took me through using Rails to generate a simple controller to
display text on a web page just fine. The problem showed up with the use
of
the database.

I set up the database, table, and fields per the article with no problem
(There was one surprise in that the article included an “important note”
to
the effect that MySQL would name the primary key ‘Id’ and I’d need to
change
it to ‘id’ to make Rails happy. In fact, MySQL named it ‘id’ so I didn’t
need to change it.)

I used ‘ruby script\generate model Recipe’ and ‘ruby script\generate
controller Recipe’ and the expected files (per the article) were
generated.
Pointing my browser at ‘http://127.0.0.1:3000/recipe/new’ resulted in a
page
being displayed with the message
“Unknown action
no action responded to new”
That’s pretty much what I expected.

The stack problem happened when I added the ‘scaffold :recipe’ line to
‘recipe_controller.rb’ Pointing my browser to the same URL now generates
the error page. It looks like somewhere over 1000 identical lines of

"c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/callba

cks.rb:236:in `initialize_without_callbacks’". That section is followed
by
a 40-50 calls that include webrick. The last line of the error message
says
“script/server:3” I can send along the page itself with all the calls
listed if that’s of any help.

Does any of this help? I’m pretty impressed with RoR based on the
article.


#12

activerecord-1.13.0
mysql (2.7, 2.6, 2.5.1)
MySQL/Ruby provides the same functions for Ruby programs that the
MySQL C API provides for C programs.

Also, as you’re on Windows - had you been aware of Instant Rails?

http://instantrails.rubyforge.org/wiki/wiki.pl

Regards,

Bill


#13

Sorry about that, Jake. When I saw your remark about “valid order” I
understood this to be a problem of inserting records in the correct
order (given database integrity constraints). Now I see that you’re
talking about referencing a record by some unique part other than the
primary key (which changes from DB to DB).

I don’t know of any trick in the books that would do what you’re
trying to–other than creating a dictionary of IDs that help
transition from the old IDs to the new ones. I wish I could be of
more help.

Regards,
Duane J.
(canadaduane)
http://blog.inquirylabs.com/

Thanks Duane-

For the moment, I’m brute forcing it. When I export the YAML, I change
the IDs into the strings of the records they represent (ie,
person.name). This is a bit easier than doing the dictionary approach,
but not as elegant.

I don’t expect to do this often, really. It’s primarily there so I can
setup “DEMO” data on a nightly basis from the same record set.

JAke


#14

On Jan 1, 2006, at 6:55 PM, Jake J. wrote:

http://blog.inquirylabs.com/
have those IDs.

Jake

Sorry about that, Jake. When I saw your remark about “valid order” I
understood this to be a problem of inserting records in the correct
order (given database integrity constraints). Now I see that you’re
talking about referencing a record by some unique part other than the
primary key (which changes from DB to DB).

I don’t know of any trick in the books that would do what you’re
trying to–other than creating a dictionary of IDs that help
transition from the old IDs to the new ones. I wish I could be of
more help.

Regards,
Duane J.
(canadaduane)
http://blog.inquirylabs.com/


#15

Greetings all,

I’m quickly getting to the “bang head here” stage so any help is
awfully,
terribly, desperately, appreciated :wink:

Looking at my previous email it hit me that I didn’t have a mySQL gem
and
needed to do ‘gem install mysql’. That added a ‘mysql 4.7’ directory
to
the ones I listed in my previous email. Cool!, I thought. Just like I
thought. There was something I hadn’t done. Unfortunately, the gem
install
ultimately failed with ‘ERROR: Failed to build gem native extension’
message
(there was more if it’s of any interest). It looked like a compile-type
set
of errors.

(QUESTION: Do I need to remove the stuff that gem installed? With the
mysql.so executable installed as noted below, is this stuff still
needed?)

While I was waiting for gem, I found the same thing Bill sent along
below
at: http://www.tmtm.org/en/mysql/ruby/ My problem is that it needs to
be
compiled and I don’t have a C compiler installed on the system I’m
working
on. Also, and not as easily addressed, it’s been so long since I did
any
serious programming, much less set up a development environment, I’d
probably just screw it up ;-(

I did some more hunting and found
http://wiki.rubyonrails.org/rails/pages/HowToUseMySQLRubyBindingsOnWin32
GREAT!!! I thought. Here it was; the executable I needed, along with
feedback from other newbies that it worked. So I put MSVCR70.dll in
c:\ruby\bin (even downloaded a new one based on one of the comments on
the
wiki) and I put mySQL.so in c:\ruby\lib\ruby\1.8\i386-mswin32 and I
rebooted
my system and…

The damn thing is still broke. I’m still getting the same stack level
too
deep error. But wait. There’s more!

There’s been an interesting (and I’m hoping informative) change in
behavior.
When I pointed my browser at http://127.0.0.1:3000/recipe/new I got the
stack overflow error. When I pointed it at http://127.0.0.1:3000/recipe
though I got a page that had a three lines on it.
Listing recipes
Title Instructions
New recipe (NOTE: this is a hyperlink)

Maybe something’s working?

When I clicked on New recipe I got the stack problem.

When I used the browser’s Back arrow, WEBrick shut down due to a
segmentation fault.

I’m optimistic about RoR and looking forward to using it. I sure would
appreciate some help getting it set up correctly.

Thanks,
Bill

Bill K." wrote:

From: “Bill W.” removed_email_address@domain.invalid

Is there supposed to be a MySQL gem installed for the example in the RoR
article to work? If so, how / where do I get it? I previously did a
'gem


#16

On Jan 2, 2006, at 6:20 PM, Bill W. wrote:

The damn thing is still broke. I’m still getting the same stack
level too
deep error. But wait. There’s more!

Generally, that would mean you have an endless recursion.

Maybe something’s working?

Yes, it would appear that ActiveRecord can talk to the DB, or that
wouldn’t
happen.


– Tom M.


#17

Hi Tom.

Tom M. wrote:

On Jan 2, 2006, at 6:20 PM, Bill W. wrote:

The damn thing is still broke. I’m still getting the same stack
level too deep error. But wait. There’s more!

Generally, that would mean you have an endless recursion.

[BW] The loop is large, but not endless.

The stack problem happened when I added the ‘scaffold :recipe’ line to
‘recipe_controller.rb’ That addition is the first and only ‘coding’
I’ve
done to this point. Pointing my browser to localhost:3000/recipe/new
generates the error page. The first 1000+ lines are identical:
"c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/callba
cks.rb:236:in “initialize_without_callbacks’”. That’s followed by
another
40-50 lines, the last 20 or so are either to WEBrick specifically or
look
related.

The last two lines of the error message are:
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/depend
encies.rb:214:in `require’
script/server:3

I can send along the whole page with all the calls listed if you’re
interested in seeing it.

There’s been an interesting (and I’m hoping informative) change in
behavior. When I pointed my browser at http://127.0.0.1:3000/recipe/new
I got the stack overflow error. When I pointed it at
http://127.0.0.1:3000/

recipe though I got a page that had a three lines on it.
Listing recipes
Title Instructions
New recipe (NOTE: this is a hyperlink)

Maybe something’s working?
Yes, it would appear that ActiveRecord can talk to the DB, or that
wouldn’t happen.

[BW] I’m going to go out on a limb here, being such a Ruby N. (I just
picked up Hibbs’ “Rolling with Ruby on Rails” article and started
working
through it yesterday). If I understand what you’re saying, then your
impression is that the problem at this point is not with the mySQL
setup,
but with Rails. Is that right? Any ideas re: where I go from here?

Thanks,
Bill


#18

It might be easier to just use Instant Rails:

http://instantrails.rubyforge.org/

Curt


#19

Oops. Sorry, no. I found that i had to increase the stacksize just
to get things to work, but also that if I had an infinite recursion, I
got the same error. I still don’t know what is causing the infinite
recursion but i thas to do with validates_associated. Try commenting
out validations until it goes away?!

On 1/2/06, Bill W. removed_email_address@domain.invalid wrote:

From: “Ian H.” removed_email_address@domain.invalid
On 1/2/06, Bill W. removed_email_address@domain.invalid wrote:

Best regards,


"Her faults were those of her race and sex; her virtues were her own.
Farewell, and if for ever - "

– “Travels with a Donkey in the Cevennes” by Robert Louis Stevenson


#20

You don’t want to increase your stack size. The problem is the infinite
recursion, you need to fix that.

http://z.iwethey.org/forums/render/content/show?contentid=233038

I had the same problem, no idea why it happens to certain people, must
be some weird interplay of the versions. The last post in this thread,
by Arkadiy:


I did manage to get my app working by loading ActiveRecord explicitly in
boot.rb, like this:

require ‘rubygems’
require_gem ‘activerecord’ # this is my new line
require ‘initializer’

I have no idea why most people don’t have this problem…

  1. I will not outsource core functions.

    . http://omega.med.yale.edu/~pcy5/misc/overlord2.htm [*]
    http://omega.med.yale.edu/~pcy5/misc/overlord2.htm