2012-06-20 77 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