2013-06-04 45 views
0

User has_many :organizations可以访问_id一个引用的对象,但不是对象直接

create_table "organizations", :force => true do |t| 
    t.string "name" 
    t.integer "founder_id" 

我可以分配founder_id,但不能访问方正(方法缺失)。

class CreateOrganizations < ActiveRecord::Migration 
def change 
create_table :organizations do |t| 
    t.string :name 
    t.belongs_to :founder, :class_name => "User" 

什么我需要改变,所以我可以访问的Oranization创始人(用户)ATTR?

回答

1

方法belongs_to的应该叫上组织类,像这样:

class Organization 
    belongs_to :founder, :class_name => 'User' 
end 

而且在迁移:

create_table :organizations do |t| 
    t.string :name 
    t.integer :founder_id 
end 
+0

哦,让我不得不向后那些试图..。 – quantumpotato

+0

我离开了迁移,更新了班级。作品,谢谢! – quantumpotato

+0

嗯..现在这是一个问题,与rails_admin:http://stackoverflow.com/questions/16929802/no-such-column-with-a – quantumpotato

相关问题