1
我正在使用Ruby on Rails 3.0.9。我試圖從關係表中獲取所有父母的列表。這是代碼。如何從關係表中獲取所有父母的列表
require 'rubygems'
gem 'activerecord', '3.0.9'
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
:database => ':memory:')
ActiveRecord::Schema.define do
create_table :people, :force => true do |t|
end
create_table :relationships, :force => true do |t|
t.integer :parent_id
t.integer :child_id
end
end
class Person < ActiveRecord::Base
end
class Relationship < ActiveRecord::Base
belongs_to :parent, :class_name => 'Person'
belongs_to :child, :class_name => 'Person'
end
child = Person.create!
parent = Person.create!
Relationship.create!(:parent_id => parent.id, :child_id => child.id)
# Person.parents_list needs to be implemented and
# I am stuck and do not know how to get to that
assert 1, Person.parents_list.size
assert parent.id, Person.parents_list.first.id
所有常量都有父母方法。因此,我正在使用parents_list而不是父母。僅供參考。 –