Active Record out of rails: still assumes id?

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

On 21/nov/06, at 14:12, Chris G. wrote:

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

Hi –

On Tue, 21 Nov 2006, Chris G. wrote:

': Mysql::Error: Unknown column ‘id’ in ‘where clause’: UPDATE results
the case?
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

unknown wrote:

Hi –

On Tue, 21 Nov 2006, Chris G. wrote:

': Mysql::Error: Unknown column ‘id’ in ‘where clause’: UPDATE results
the case?
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

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

Chris G. wrote:

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 D. 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 :slight_smile:
Cheers,
V.-

On 11/21/06, Gabriele M. [email protected] wrote:

ActiveRecord what is the key column, as in:
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

On 11/22/06, Ezra Z. [email protected] wrote:

And no, I have no affiliation whatsoever with Andy or Dave :slight_smile:
http://svn.devjavu.com/merb/lib/tasks/db.rake

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

Great stuff!

On Nov 21, 2006, at 11:41 AM, Damphyr wrote:

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 D. 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 :slight_smile:
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 Z.
– Lead Rails Evangelist
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs