Forum: Ruby Railless ActiveRecord

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ari B. (Guest)
on 2007-08-01 06:38
(Received via mailing list)
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.
Gregory B. (Guest)
on 2007-08-01 06:52
(Received via mailing list)
On 7/31/07, Ari B. <removed_email_address@domain.invalid> wrote:
> 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/ruby/2007/06/21/ho...

See the ActiveRecord::Schema documentation for more help.
John J. (Guest)
on 2007-08-01 07:01
(Received via mailing list)
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.
Ari B. (Guest)
on 2007-08-01 07:15
(Received via mailing list)
On Jul 31, 2007, at 10:50 PM, Gregory B. wrote:

> 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/ho...
> 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.
Ari B. (Guest)
on 2007-08-01 07:19
(Received via mailing list)
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
John J. (Guest)
on 2007-08-01 16:05
(Received via mailing list)
On Jul 31, 2007, at 10:18 PM, Ari B. 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.
Ari B. (Guest)
on 2007-08-01 18:48
(Received via mailing list)
On Aug 1, 2007, at 8:04 AM, John J. wrote:

> 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.
Giles B. (Guest)
on 2007-08-01 21:30
(Received via mailing list)
> > 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

> Just kidding,

Doh!

btw Gregory B.'s article is very good.

--
Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
John J. (Guest)
on 2007-08-01 22:45
(Received via mailing list)
On Aug 1, 2007, at 9:47 AM, Ari B. wrote:

>>
> 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.
Ari B. (Guest)
on 2007-08-01 23:17
(Received via mailing list)
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
Ari B. (Guest)
on 2007-08-01 23:25
(Received via mailing list)
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.
Gregory B. (Guest)
on 2007-08-01 23:40
(Received via mailing list)
On 8/1/07, Ari B. <removed_email_address@domain.invalid> wrote:
>
>         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. :)
Ari B. (Guest)
on 2007-08-01 23:46
(Received via mailing list)
On Aug 1, 2007, at 3:38 PM, Gregory B. wrote:

>> HOWEVER, when I use:
>>
>>         require 'rubygems'
>>         gem 'activerecord'
>>
>> these warning do not show. Why is that?
>
> 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
Gregory B. (Guest)
on 2007-08-01 23:52
(Received via mailing list)
On 8/1/07, Ari B. <removed_email_address@domain.invalid> wrote:

> > Because gem() doesn't require anything.
>
> 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  :-/
Luke K. (Guest)
on 2007-08-01 23:55
(Received via mailing list)
On Jul 31, 2007, at 9:37 PM, Ari B. 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.

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/trun...
https://reductivelabs.com/trac/puppet/browser/trun...

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

  --
  If computers get too powerful, we can organize them into a
committee --
  that will do them in.    -- Bradley's Bromide
  ---------------------------------------------------------------------
  Luke K. | http://reductivelabs.com | http://madstop.com
Ezra Z. (Guest)
on 2007-08-02 00:11
(Received via mailing list)
On Aug 1, 2007, at 12:51 PM, Luke K. wrote:

>
> https://reductivelabs.com/trac/puppet/browser/trun...
> rails.rb
> https://reductivelabs.com/trac/puppet/browser/trun...
>
> 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 Z.
-- Founder & Ruby Hacker
-- removed_email_address@domain.invalid
-- Engine Y., Serious Rails Hosting
-- (866) 518-YARD (9273)
Luke K. (Guest)
on 2007-08-02 00:30
(Received via mailing list)
On Aug 1, 2007, at 3:10 PM, Ezra Z. 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.

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)
  ---------------------------------------------------------------------
  Luke K. | http://reductivelabs.com | http://madstop.com
This topic is locked and can not be replied to.