Active Record out of rails: still assumes id?

Discussion in 'Ruby' started by Chris Gallagher, Nov 21, 2006.

  1. 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

    --
    Posted via http://www.ruby-forum.com/.
     
    Chris Gallagher, Nov 21, 2006
    #1
    1. Advertising

  2. On 21/nov/06, at 14:12, Chris Gallagher 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

    --
    Gabriele Marrone
     
    Gabriele Marrone, Nov 21, 2006
    #2
    1. Advertising

  3. Chris Gallagher

    Guest

    Hi --

    On Tue, 21 Nov 2006, Chris Gallagher wrote:

    > 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 |
    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
     
    , Nov 21, 2006
    #3
  4. unknown wrote:
    > Hi --
    >
    > On Tue, 21 Nov 2006, Chris Gallagher 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.

    --
    Posted via http://www.ruby-forum.com/.
     
    Chris Gallagher, Nov 21, 2006
    #4
  5. Chris Gallagher

    Damphyr Guest

    Chris Gallagher 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 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.-

    --
    http://www.braveworld.net/riva
     
    Damphyr, Nov 21, 2006
    #5
  6. On 11/21/06, Gabriele Marrone <> wrote:
    >
    > On 21/nov/06, at 14:12, Chris Gallagher 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



    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
     
    Leslie Viljoen, Nov 21, 2006
    #6
  7. On Nov 21, 2006, at 11:41 AM, Damphyr wrote:

    > Chris Gallagher 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 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
    --
    -- Engine Yard, Serious Rails Hosting
    -- (866) 518-YARD (9273)
     
    Ezra Zygmuntowicz, Nov 22, 2006
    #7
  8. On 11/22/06, Ezra Zygmuntowicz <> wrote:
    >
    > On Nov 21, 2006, at 11:41 AM, Damphyr wrote:
    >
    > > Chris Gallagher 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 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



    Great stuff!
     
    Leslie Viljoen, Nov 23, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Heinemeier Hansson
    Replies:
    2
    Views:
    185
    Michael Neumann
    May 31, 2004
  2. David Heinemeier Hansson
    Replies:
    0
    Views:
    147
    David Heinemeier Hansson
    Aug 1, 2004
  3. David Heinemeier Hansson
    Replies:
    3
    Views:
    177
    Mauricio Fernández
    Aug 6, 2004
  4. David Heinemeier Hansson
    Replies:
    0
    Views:
    272
    David Heinemeier Hansson
    Sep 5, 2004
  5. Xeno Campanoli
    Replies:
    1
    Views:
    145
    Xeno Campanoli
    Jul 18, 2008
Loading...

Share This Page