User has_many :organizations
create_table "organizations", :force => true do |t|
t.string "name"
t.integer "founder_id"
我可以指定founder_id,但不能访问Founder(缺少方法)。
class CreateOrganizations < ActiveRecord::Migration
def change
create_table :organizations do |t|
t.string :name
t.belongs_to :founder, :class_name => "User"
我需要更改哪些内容才能访问Oranization上的创始人(用户)attr?
答案 0 :(得分:1)
应该在Organization类上调用方法belongs_to,如下所示:
class Organization
belongs_to :founder, :class_name => 'User'
end
在迁移中:
create_table :organizations do |t|
t.string :name
t.integer :founder_id
end