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.
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-08-01 04: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.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-08-01 04:52
(Received via mailing list)
On 7/31/07, Ari Brown <ari@aribrown.com> 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.
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-08-01 05: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.
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-08-01 05:15
(Received via mailing list)
On Jul 31, 2007, at 10:50 PM, Gregory Brown 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.
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-08-01 05: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
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-08-01 14:05
(Received via mailing list)
On Jul 31, 2007, at 10:18 PM, Ari Brown 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.
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-08-01 16:48
(Received via mailing list)
On Aug 1, 2007, at 8:04 AM, John Joyce 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.
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2007-08-01 19: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 Brown's article is very good.

--
Giles Bowkett

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-08-01 20:45
(Received via mailing list)
On Aug 1, 2007, at 9:47 AM, Ari Brown 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.
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-08-01 21: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
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-08-01 21: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.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-08-01 21:40
(Received via mailing list)
On 8/1/07, Ari Brown <ari@aribrown.com> 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. :)
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-08-01 21:46
(Received via mailing list)
On Aug 1, 2007, at 3:38 PM, Gregory Brown 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
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-08-01 21:52
(Received via mailing list)
On 8/1/07, Ari Brown <ari@aribrown.com> 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  :-/
Ea6905beef63a84a75793f4f72cb8ff7?d=identicon&s=25 Luke Kanies (Guest)
on 2007-08-01 21:55
(Received via mailing list)
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.

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 Kanies | http://reductivelabs.com | http://madstop.com
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2007-08-01 22:11
(Received via mailing list)
On Aug 1, 2007, at 12:51 PM, Luke Kanies 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 Zygmuntowicz
-- Founder & Ruby Hacker
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
Ea6905beef63a84a75793f4f72cb8ff7?d=identicon&s=25 Luke Kanies (Guest)
on 2007-08-01 22:30
(Received via mailing list)
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.

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