Can access _id of a references object but not the object directly

旧城冷巷雨未停 提交于 2019-12-13 07:13:39

问题


User has_many :organizations

create_table "organizations", :force => true do |t|
  t.string   "name"
  t.integer  "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"

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


回答1:


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

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

And in migration:

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


来源:https://stackoverflow.com/questions/16928190/can-access-id-of-a-references-object-but-not-the-object-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!