Active Record out of rails: still assumes id?

C

Chris Gallagher

Hi,

Im currently writing a ruby script that will connect to a database using
active record and then insert data that has been scraped from certain
websites.

A problem that I have run into is that when i try to use the .save
command i receive the following error:

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:
': Mysql::Error: Unknown column 'id' in 'where clause': UPDATE results
SET `module_name` = '--- !ruby/object:
\nentity_filter: \nnormalized: \nparent: \nraw: true\nstring:
henry-mobile-server\nunnormalized: \n', `build
tp://10.37.150.55:8080', `build_status_since` = '12:42', `build_number`
= 'build.16', `last_failure` = NULL,
ss` = '20/11/06' WHERE id = NULL (ActiveRecord::StatementInvalid)

This is clearly due to the fact that I dont have any id fields in my
database as i thought that active record would do something similar to
rails and that i wouldnt have to define them? Obviously this isnt quite
the case?

the code that im using for this segment of the script is as follows:

Result.find:)all,:conditions => ["build_url = ?",
records.build_url]).each do |b|
puts b.id
b.build_status_since = build_status_since
b.save
end

Is there a work around for this problem or am I missing the problem
completely here?

Cheers,

Chris
 
G

Gabriele Marrone

This is clearly due to the fact that I dont have any id fields in my
database as i thought that active record would do something similar to
rails and that i wouldnt have to define them? Obviously this isnt
quite
the case?

I guess your table will have a key, anyway. You just have to tell
ActiveRecord what is the key column, as in:

class Person < ActiveRecord::Base
set_primary_key :full_name # overrides id with full_name
end

Some times you will need a composite primary key, but ActiveRecord
doesn't support them by default. If you need them, however, you can
install the composite_primary_keys gem (http://
compositekeys.rubyforge.org/) and then do something like this:

class Person < ActiveRecord::Base
set_primary_keys :first_name, :last_name
end
 
D

dblack

Hi --

Hi,

Im currently writing a ruby script that will connect to a database using
active record and then insert data that has been scraped from certain
websites.

A problem that I have run into is that when i try to use the .save
command i receive the following error:

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:
': Mysql::Error: Unknown column 'id' in 'where clause': UPDATE results
SET `module_name` = '--- !ruby/object:
\nentity_filter: \nnormalized: \nparent: \nraw: true\nstring:
henry-mobile-server\nunnormalized: \n', `build
tp://10.37.150.55:8080', `build_status_since` = '12:42', `build_number`
= 'build.16', `last_failure` = NULL,
ss` = '20/11/06' WHERE id = NULL (ActiveRecord::StatementInvalid)

This is clearly due to the fact that I dont have any id fields in my
database as i thought that active record would do something similar to
rails and that i wouldnt have to define them? Obviously this isnt quite
the case?

the code that im using for this segment of the script is as follows:

Result.find:)all,:conditions => ["build_url = ?",
records.build_url]).each do |b|
puts b.id
b.build_status_since = build_status_since
b.save
end

Is there a work around for this problem or am I missing the problem
completely here?

The id field is automatically created when you use ActiveRecord
Migrations. Otherwise, you have to create it yourself.


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
C

Chris Gallagher

unknown said:
Hi --



The id field is automatically created when you use ActiveRecord
Migrations. Otherwise, you have to create it yourself.


David

I solved this by just throwing in the id field. Wasnt really a big thing
to do.
 
D

Damphyr

Chris said:
unknown wrote:

I solved this by just throwing in the id field. Wasnt really a big thing
to do.
I'm actually using active record in a similar fashion. I just want the
ORM (with the thinking that at some point in the very near future I can
use Rails to give me a nice frontend for my data).

The one thing I miss is the whole Rails infrastructure for putting the
db schema in YAML and automating the migrations, but I would still
recommend using migrations, even if you have to code them.
That way all the nice :has_many etc. works without thinking.
That said, I still structured the app using the Rails conventions so
that when I use Rails I can dump my coded migrations and leverage the
scripts.
Btw. Agile Web Development with Rails, 2nd ed (the beta book) is an
enormous help for figuring things out, even if you just want to use
active record :).
And no, I have no affiliation whatsoever with Andy or Dave :)
Cheers,
V.-
 
L

Leslie Viljoen

I guess your table will have a key, anyway. You just have to tell
ActiveRecord what is the key column, as in:

class Person < ActiveRecord::Base
set_primary_key :full_name # overrides id with full_name
end

Some times you will need a composite primary key, but ActiveRecord
doesn't support them by default. If you need them, however, you can
install the composite_primary_keys gem (http://
compositekeys.rubyforge.org/) and then do something like this:

class Person < ActiveRecord::Base
set_primary_keys :first_name, :last_name
end


Though if you do use set_primary_key, remember that in the model
object, the field will still be called 'id' even though it's mapped to
another column in the table. You also then assume responsibility for
incrementing and setting that ID before every save, since the ID can
be in any format.

I have:


class ArchiveLog < MsSqlTable
set_table_name "ArchiveLog"
set_primary_key "ArchiveLogID"
end

...

next_id = ArchiveLog.maximum:)ArchiveLogID)
if next_id.nil?
next_id = 0
else
next_id += 1
end

archiveLog = ArchiveLog.new
archiveLog.id = next_id
archiveLog.ArchiveDate = @startTime
archiveLog.CurrentDataStartDate = @archiveBorderTime

..etc.


Les


--
Man's unfailing capacity to believe what he prefers to be true rather
than what the evidence shows to be likely and possible has always
astounded me. We long for a caring Universe which will save us from
our childish mistakes, and in the face of mountains of evidence to the
contrary we will pin all our hopes on the slimmest of doubts. God has
not been proven not to exist, therefore he must exist.

- Prokhor Zakharov
 
E

Ezra Zygmuntowicz

I'm actually using active record in a similar fashion. I just want
the ORM (with the thinking that at some point in the very near
future I can use Rails to give me a nice frontend for my data).

The one thing I miss is the whole Rails infrastructure for putting
the db schema in YAML and automating the migrations, but I would
still recommend using migrations, even if you have to code them.
That way all the nice :has_many etc. works without thinking.
That said, I still structured the app using the Rails conventions
so that when I use Rails I can dump my coded migrations and
leverage the scripts.
Btw. Agile Web Development with Rails, 2nd ed (the beta book) is an
enormous help for figuring things out, even if you just want to use
active record :).
And no, I have no affiliation whatsoever with Andy or Dave :)
Cheers,
V.-


I was able to get ActiveRecord migrations working outside of rails
by tweaking the rake tasks and a little scripting. My little web
framework called Merb uses AR but does migrations in a similar way to
rails. Here are the relevant files to look at if you want to do
something similar:

http://svn.devjavu.com/merb/lib/tasks/db.rake

http://svn.devjavu.com/merb/examples/sample_app/script/new_migration

Cheers-

-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top