Railless ActiveRecord

A

Ari Brown

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

Gregory Brown

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?

I show how to do this on page 2 of my "How to build simple console
apps with Ruby and ActiveRecord" article

http://www.oreillynet.com/pub/a/rub...e-apps-with-ruby-and-activerecord.html?page=2

See the ActiveRecord::Schema documentation for more help.
 
J

John Joyce

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

Ari Brown

I show how to do this on page 2 of my "How to build simple console
apps with Ruby and ActiveRecord" article

http://www.oreillynet.com/pub/a/ruby/2007/06/21/how-to-build-simple-
console-apps-with-ruby-and-activerecord.html?page=2

See the ActiveRecord::Schema documentation for more help.

Thanks! The ActiveRecord::Schema part was what I needed.

I'm fairly certain I'm blind, because I was scouring your page for
that and found nothing.

~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.
 
A

Ari Brown

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
 
J

John Joyce

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

Ari Brown

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

Giles Bowkett

Active Record (and Rails in general from that) uses a very clever
Thanks! What do I do if I have:

has_many :platypi?

Inflector.inflections do |inflect|
inflect.irregular 'platypus', 'platypi'
end
Just kidding,

Doh!

btw Gregory Brown's article is very good.
 
J

John Joyce

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

Ari Brown

Ok. I have another Problem

Whenever I use my require statements:

require 'rubygems'
require 'active_record'

there is always a bunch of warnings spat out by activerecord.

HOWEVER, when I use:

require 'rubygems'
gem 'activerecord'

these warning do not show. Why is that?

And of late, when I use:

require 'rubygems'
gem 'activerecord'

I get a require error saying not that the gem 'activerecord' could
not be found,
but that I have an unitiliazed constant 'ActiveRecord::Base'.

Bwah???

thanks,
aRi
-------------------------------------------|
Nietzsche is my copilot
 
A

Ari Brown

BTW, here is the exact warnings spat by ActiveRecord:
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/validations.rb:334: warning: `*' interpreted as
argument prefix
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/validations.rb:363: warning: `*' interpreted as
argument prefix
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/migration.rb:226: warning: instance variable
@ignore_new_methods not initialized
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/migration.rb:226: warning: instance variable
@ignore_new_methods not initialized
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/attribute_methods.rb:7: warning: `*' interpreted as
argument prefix
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/abstract/
connection_specification.rb:41: warning: method redefined; discarding
old allow_concurrency=
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/postgresql_adapter.rb:390: warning:
`&' interpreted as argument prefix


I'm using Ruby 1.8.6 on powerpc darwin (Mac OS X)

and the missing gem:
new:6: uninitialized constant ActiveRecord (NameError)


aRi
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
 
G

Gregory Brown

Ok. I have another Problem

Whenever I use my require statements:

require 'rubygems'
require 'active_record'

there is always a bunch of warnings spat out by activerecord.

HOWEVER, when I use:

require 'rubygems'
gem 'activerecord'

these warning do not show. Why is that?

Because gem() doesn't require anything.
And of late, when I use:

require 'rubygems'
gem 'activerecord'

I get a require error saying not that the gem 'activerecord' could
not be found,
but that I have an unitiliazed constant 'ActiveRecord::Base'.

See above. :)
 
A

Ari Brown

Because gem() doesn't require anything.

Bwah? But I've used it before! How can this be?


---------------------------------------------------------------|
~Ari
"I don't suffer from insanity. I enjoy every minute of it" --1337est
man alive
 
G

Gregory Brown

Bwah? But I've used it before! How can this be?

This behaviour has been on the way out since before 0.9.0. Just use:

require "rubygems"
require "active_record"

gem() is meant to be used only to lock a gem to a specific version,
not to load a library.

As far as the warnings go, it's because the Rails core team is too
cool for ruby -w :-/
 
L

Luke Kanies

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.

FWIW, I'm also using ActiveRecord outside of Rails, it might help to
look through my examples, including how I handle my migrations:

https://reductivelabs.com/trac/puppet/browser/trunk/lib/puppet/rails.rb
https://reductivelabs.com/trac/puppet/browser/trunk/lib/puppet/rails

Note that my usage of ActiveRecord seems to spawn a new connection on
every request, which I can't seem to fix.
 
E

Ezra Zygmuntowicz

FWIW, I'm also using ActiveRecord outside of Rails, it might help
to look through my examples, including how I handle my migrations:

https://reductivelabs.com/trac/puppet/browser/trunk/lib/puppet/
rails.rb
https://reductivelabs.com/trac/puppet/browser/trunk/lib/puppet/rails

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.

Cheers-
-- Ezra Zygmuntowicz
-- Founder & Ruby Hacker
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 
L

Luke Kanies

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top