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:
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?
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
···
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?
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:
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.
': 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.
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
On 11/21/06, Gabriele Marrone <gabriele.marrone@gmail.com> 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
--
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.
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:
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.-
On 11/22/06, Ezra Zygmuntowicz <ezmobius@gmail.com> 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: