2012-06-20 117 views
14

民间 我试图使用没有导轨的活动记录,并且看起来不能让has_many正常工作。我从来没有尝试过使用没有导轨的活动记录。我可以从单个表中查询,但关系似乎没有工作。任何人都可以快速浏览一下,看看我是否缺少任何东西。这里是存根如何使用没有导轨的活动记录

#!/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 :users 
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.problem.desc 
end 

# run some methods 
show_single_item # works 
show_all_items # works 
check_has_many # not working 


------ 

here is the schema of users and problems from the database 

sqlite> .schema users 
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name"  varchar(255), "last_name" varchar(255)); 

sqlite> .schema problems 
CREATE TABLE "problems" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "desc" varchar(255)); 

and some selects to show some data from the tables 

sqlite> select * from users; 
2|mike|smit 
3|mike|wilson 

sqlite> select * from problems; 
1||first problem 
2||it went 
3||this is a new problem 
4||some more junk data 

,这里是错误

ruby-1.8.7-p352/gems/activemodel-3.2.3/lib/active_model/attribute_methods.rb:407:in `method_missing': \ 
undefined method `problem' for #<User id: 2, first_name: "mike", last_name: "smit"> (NoMethodError) 
     from /home/wileybd/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.2.3/lib/active_record/attribute_methods.rb:148:in `method_missing' 
     from ./main.rb:38:in `check_has_many' 
     from ./main.rb:44 

任何帮助,将不胜感激。

+0

如果你正在运行活动记录3.2.3(看起来像)我会建议使用RVM升级红宝石1.8.7到1.9.3 –

回答

2

您提供的堆栈跟踪说明错误是什么。它在check_has_many方法:

def check_has_many 
    user = User.find(:first) 
    puts user.problem.desC# <==== should be user.problems 
end 

你的用户有很多问题,所以它必须是复数:

def check_has_many 
    user = User.find(:first) 
    puts user.problems.first.desC# <==== do this instead 
end 

而且,你belongs_to的:在问题模型的用户关系,应该是单数:

class Problem < ActiveRecord::Base 
    belongs_to :user # <=== singular, not :users 
end 
+0

抱歉,我应该显示我是如何尝试的,但我改变了它回到刚刚发布的内容,现在是错误 – user740970

+0

.rvm/gems/ruby​​-1.8.7-p352/gems/activerecord-3.2.3/lib/active_record/relation/delegation.rb:45:in'method_missing' :[]的未定义方法'desc':ActiveRecord :: Relation(NoMethodError) from /home/wileybd/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.2.3/lib/active_record/associations /collection_proxy.rb:100:in'发送' from /home/wileybd/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:100 :在'method_missing' from ./main.rb:38:in'check_has_many' from ./main.rb:44 – user740970

+0

所以我想respond_to没有发生在那种方法? – user740970

6

我想我知道你在做什么。如果我没有弄错,你想显示给定用户的每个问题的desc值。

一个简单的方法来完成你需要的是你的最后2点相结合的方法:

user = User.first 
user.problems.each do |pr| 
    puts pr.desc 
end 

您在代码中所遇到的问题是,语义你在说什么“显示的说明问题(注意,这是奇)用户”,而不是说‘显示用户有每一个问题’,其中如果有可能的描述应该是这样的:

puts user.problems.descs # This will not work 

但是,这只是没有办法的办法有用。还有就是,虽然,一个new method,您可以使用:

puts user.problems.pluck(:desc) 

这种方法会产生对用户的每个问题的递减值的数组。您可以使用输出来打印您喜欢的方式。

+1

+1 for pluck - 注意:从3.2.1+ –

+0

pepe可用,谢谢我能够使所有的工作现在。我又一次忘记了我正在收集回来。我也不知道采摘,再次感谢。 – user740970

+0

@ user740970不客气。我很高兴我能帮上忙。 – pepe