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 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?
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.
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.
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.
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.
Just kidding,
Thanks,
Ari
--------------------------------------------|
If you’re not living on the edge,
then you’re just wasting space.
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.
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
Note that my usage of ActiveRecord seems to spawn a new connection
on every request, which I can’t seem to fix.
Luke-
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.
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.
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!
–
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)