# Active Record out of rails: still assumes id?

**URL:** <https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816>\
**Category:** Ruby\
**Created:** [November 21, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816 "2006-11-21T14:12:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 21, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816/1 "2006-11-21T14:12:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 21, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816/2 "2006-11-21T14:23:01Z")

</div>

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/](http://compositekeys.rubyforge.org/)) and then do something like this:

class Person \< ActiveRecord::Base  
set\_primary\_keys :first\_name, :last\_name  
end

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 21, 2006, 2:27pm UTC](https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816/3 "2006-11-21T14:27:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 21, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816/4 "2006-11-21T16:49:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 21, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816/5 "2006-11-21T20:42:43Z")

</div>

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 🙂  
Cheers,  
V.-

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 21, 2006, 11:43pm UTC](https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816/6 "2006-11-21T23:43:20Z")

</div>

On 11/21/06, Gabriele M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 23, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816/7 "2006-11-23T16:28:31Z")

</div>

On 11/22/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > And no, I have no affiliation whatsoever with Andy or Dave 🙂  
> > [http://svn.devjavu.com/merb/lib/tasks/db.rake](http://svn.devjavu.com/merb/lib/tasks/db.rake)
> 
> [http://svn.devjavu.com/merb/examples/sample\_app/script/new\_migration](http://svn.devjavu.com/merb/examples/sample_app/script/new_migration)

Great stuff!

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 22, 2006, 1:05am UTC](https://www.ruby-forum.com/t/active-record-out-of-rails-still-assumes-id/76816/8 "2006-11-22T01:05:30Z")

</div>

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 🙂  
> 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/lib/tasks/db.rake)

[http://svn.devjavu.com/merb/examples/sample\_app/script/new\_migration](http://svn.devjavu.com/merb/examples/sample_app/script/new_migration)

Cheers-

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)
