可以访问的引用_id对象,但不是直接的对象 [英] Can access _id of a references object but not the object directly

查看:188
本文介绍了可以访问的引用_id对象,但不是直接的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户的has_many:组织

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

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

I can assign founder_id, but not access Founder (method missing).

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

什么我需要改变这样我就可以访问创始人(用户)上Oranization ATTR?

What do I need to change so I can access the founder (a User) attr on an Oranization?

推荐答案

方法belongs_to的应该算得上一流的组织,像这样的:

Method belongs_to should be called on Organization class, like this:

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

和迁移:

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

这篇关于可以访问的引用_id对象,但不是直接的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆