failry simple(?) active record question.

K

Kyle Schmitt

I'm plodding my way through active record right now, and running into
some things that seem odd.

Take a really simple example
#!/usr/bin/ruby
require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection:)adapter => "sqlite3",
:database => "foo.db")

ActiveRecord::Schema.define() do
create_table :containers do |table|
table.column :name, :string
end
create_table :things do |table|
table.column :description, :string
end
end

class Container<ActiveRecord::Base
has_many :things
end

class Thing<ActiveRecord::Base
belongs_to :container
end

Container.create:)name=>"Bucket")
bucket=Container.find_by_name("Bucket")
pocket=Container.create:)name=>"Pocket")

bucket.save
pocket.save

Container.find:)all)
#this will find both containers

Thing.find:)all)
#empty, as expected

bucket.things.create:)description=>"fish")
pocket.things.create:)description=>"lint")

Thing.find:)all)
#looks good...

#but howcome
pocket.things.find:)all)
#throws some huge error instead of finding the things in the pocket?


Sorry for the overly newbie type question :) I just really thought
ActiveRecord was supposed to take care of all this stuff.

Thanks,
Kyle
 
Y

yermej

I'm plodding my way through active record right now, and running into
some things that seem odd.

Take a really simple example
#!/usr/bin/ruby
require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection:)adapter => "sqlite3",
:database => "foo.db")

ActiveRecord::Schema.define() do
create_table :containers do |table|
table.column :name, :string
end
create_table :things do |table|
table.column :description, :string
end
end

class Container<ActiveRecord::Base
has_many :things
end

class Thing<ActiveRecord::Base
belongs_to :container
end

Container.create:)name=>"Bucket")
bucket=Container.find_by_name("Bucket")
pocket=Container.create:)name=>"Pocket")

bucket.save
pocket.save

Container.find:)all)
#this will find both containers

Thing.find:)all)
#empty, as expected

bucket.things.create:)description=>"fish")
pocket.things.create:)description=>"lint")

Thing.find:)all)
#looks good...

#but howcome
pocket.things.find:)all)
#throws some huge error instead of finding the things in the pocket?

Sorry for the overly newbie type question :) I just really thought
ActiveRecord was supposed to take care of all this stuff.

Thanks,
Kyle

What error do you get? That would help in diagnosing the problem.

You might also want to ask the Ruby on Rails list. I know ActiveRecord
doesn't have to be used with Rails, but they probably have more people
there with ActiveRecord experience.

Jeremy
 
R

Ryan Davis

I'm plodding my way through active record right now, and running into
some things that seem odd.

AR questions should go to the rails list... that's the group that
writes and supports AR.
 
K

Kyle Schmitt

OK. I'll repost it there. As I really wanna just use AR and not
rails ;) I had hopes for this list.

Heck maybe this just means if I'm having this much trouble I should
write all my findings up as a few good examples and howto's.

Thanks
--Kyle
 

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

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top