问题
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