So I have my ActiveRecord connection. All is good. I am connecting to an SQLite3 database, which is working out pretty well for me. I think.
So my story is this: When I tried to use the table "characters", abstracted from class Character, I get an error:
`table_structure': ActiveRecord::StatementInvalid (ActiveRecord::StatementInvalid)
I did some poking around and actually read the error message, and I'm getting this error because there is no table characters (right??)
So how do I create these tables in ActiveRecord?
Thanks,
aRi
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
I show how to do this on page 2 of my "How to build simple console
apps with Ruby and ActiveRecord" article
See the ActiveRecord::Schema documentation for more help.
···
On 7/31/07, Ari Brown <ari@aribrown.com> wrote:
Hey all. More ActiveRecord trouble.
So I have my ActiveRecord connection. All is good. I am connecting to
an SQLite3 database, which is working out pretty well for me. I think.
So my story is this: When I tried to use the table "characters",
abstracted from class Character, I get an error:
`table_structure': ActiveRecord::StatementInvalid
(ActiveRecord::StatementInvalid)
I did some poking around and actually read the error message, and I'm
getting this error because there is no table characters (right??)
So how do I create these tables in ActiveRecord?
Rails uses something called migrations.
A migration is a Ruby file that changes the database schema.
It is basically a Ruby transliteration of SQL and can include SQL for things ActiveRecord doesn't support directly.
I haven't yet used AR outside of Rails (but want to!)
Rails includes Rake (Ruby make) tasks specifically for running migrations or removing them. Migrations can be done incrementally. You can also use the Rails console to fiddle with the db via activerecord, but I don't know if you can access the Rails console (basically it is irb on Rails crack) outside of a Rails setting.
If you get stuck with AR look at api.rubyonrails.com
it is the docs for all of Rails, but ActiveRecord is a big part of that.
And do pick up the AWDWR book because a large chunk of it is all about Active Record and is really useful as a reference.
···
On Jul 31, 2007, at 9:37 PM, Ari Brown wrote:
Hey all. More ActiveRecord trouble.
So I have my ActiveRecord connection. All is good. I am connecting to an SQLite3 database, which is working out pretty well for me. I think.
So my story is this: When I tried to use the table "characters", abstracted from class Character, I get an error:
`table_structure': ActiveRecord::StatementInvalid (ActiveRecord::StatementInvalid)
I did some poking around and actually read the error message, and I'm getting this error because there is no table characters (right??)
So how do I create these tables in ActiveRecord?
Thanks,
aRi
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
Note that my usage of ActiveRecord seems to spawn a new connection on every request, which I can't seem to fix.
···
On Jul 31, 2007, at 9:37 PM, Ari Brown wrote:
Hey all. More ActiveRecord trouble.
So I have my ActiveRecord connection. All is good. I am connecting to an SQLite3 database, which is working out pretty well for me. I think.
--
If computers get too powerful, we can organize them into a committee --
that will do them in. -- Bradley's Bromide
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com
Do you have ActiveRecord::Base.allow_concurrency = true ? If so AR uses one database connection per thread so I assume you have a multithreaded server. The way to get rid of old connections is to call ActiveRecord::Base.verify_active_connections! every 10-20 requests. It will cleanup all the old connections whos threads have finished.
If a category has_many :stories
then table stories should have a column named category_id
Generally, this association is followed with story belongs_to :category for a one to many relationship.
There are also one to one relationships using has_one and belongs_to
Active Record (and Rails in general from that) uses a very clever system of English pluralization for table names and associations rely on singular/plural rules.
···
On Jul 31, 2007, at 10:18 PM, Ari Brown wrote:
Also, is there anything special I need to do when creating a column that I'm using has_many on?
Thanks,
aRi
-------------------------------------------|
Nietzsche is my copilot
I do have concurrency set to be allowed, and yes, I'm using a multithreaded server.
I didn't know about the verify method, so I'll check that out. Thanks!
···
On Aug 1, 2007, at 3:10 PM, Ezra Zygmuntowicz wrote:
Do you have ActiveRecord::Base.allow_concurrency = true ? If so AR uses one database connection per thread so I assume you have a multithreaded server. The way to get rid of old connections is to call ActiveRecord::Base.verify_active_connections! every 10-20 requests. It will cleanup all the old connections whos threads have finished.
--
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
-- (attributed to) Brian W. Kernighan (unconfirmed)
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com
Just kidding,
Thanks,
Ari
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
···
On Aug 1, 2007, at 8:04 AM, John Joyce wrote:
Yes.
given tables, say:
categories
stories
If a category has_many :stories
then table stories should have a column named category_id
Generally, this association is followed with story belongs_to :category for a one to many relationship.
There are also one to one relationships using has_one and belongs_to
Active Record (and Rails in general from that) uses a very clever system of English pluralization for table names and associations rely on singular/plural rules.
> Active Record (and Rails in general from that) uses a very clever
> system of English pluralization for table names and associations
> rely on singular/plural rules.
Thanks! What do I do if I have:
has_many :platypi?
Inflector.inflections do |inflect|
inflect.irregular 'platypus', 'platypi'
end
Ari,
You will be surprised how often the inflector is right about plurals /singulars, but given the number of oddballs in English, it's often better to just choose something that pluralizes easily rather than writing inflection rules. If you wanted to use another language, say japanese which does actually have plural forms that are rarely used or needed, you could define your pluralization rules to end in the appropriate ending.
···
On Aug 1, 2007, at 9:47 AM, Ari Brown wrote:
On Aug 1, 2007, at 8:04 AM, John Joyce wrote:
Yes.
given tables, say:
categories
stories
If a category has_many :stories
then table stories should have a column named category_id
Generally, this association is followed with story belongs_to :category for a one to many relationship.
There are also one to one relationships using has_one and belongs_to
Active Record (and Rails in general from that) uses a very clever system of English pluralization for table names and associations rely on singular/plural rules.
Thanks! What do I do if I have:
has_many :platypi?
Just kidding,
Thanks,
Ari
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.