Folks Im trying to use active record without rails and cant seem to get
has_many working properly. Ive never tried using active record without
rails. I can query from single tables but the relationships dont seem to
be working. Could anyone take a quick glance and see if im missing
anything. Here is the stub
#!/usr/bin/ruby
require 'rubygems'
gem 'activerecord'
require 'sqlite3'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'test.db'
)
class User < ActiveRecord::Base
has_many :problems
end
class Problem < ActiveRecord::Base
belongs_to :user
end
def show_single_item
pr = Problem.find(:first)
puts "showing first problem from the db below", pr.desc
end
def show_all_items
pr = Problem.find(:all)
puts "showing all problems from the db below"
pr.each do |a|
puts a.desc
end
end
def check_has_many
user = User.find(:first)
puts user.problems.desc
end
# run some methods
show_single_item # works
show_all_items # works
check_has_many # not working
sql info
.schema users
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"first_name" varchar(255), "last_name" varchar(255));
.schema problems
CREATE TABLE "problems" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT
NULL, "user_id" integer, "desc" varchar(255));
select * from users;
2|mike|smit
3|mike|wilson
select * from users;
2|mike|smit
3|mike|wilson
select * from problems;
1||first problem
2||it went
3||this is a new problem
4||this is a new problem again
and the error
gems/ruby-1.8.7-p352/gems/activerecord-3.2.3/lib/active_record/relation/delegation.rb:45:in
`method_missing': undefined method `desc' for :ActiveRecord::Relation
(NoMethodError)
from
/home/someuser/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:100:in
`send'
from
/home/someuser/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:100:in
`method_missing'
from ./main.rb:38:in `check_has_many'
from ./main.rb:44
also in rails console i dont have any issues when i created the same
rails test app.
any help would be appreciated
···
--
Posted via http://www.ruby-forum.com/\.