我在2个资源之间有一对多关联:Discovery和Matter
class Discovery < ActiveRecord::Base
belongs_to :matter
end
class Matter < ActiveRecord::Base
has_many :discoveries
end
我的路线文件有:
resources :matters do
resources :discoveries
end
我的迁移文件如下:
class CreateDiscoveries < ActiveRecord::Migration
def change
create_table :discoveries do |t|
t.string :aws_url
t.string :upload_file_path
t.attachment :upload
t.integer :matter_id
t.string :direct_upload_url
t.boolean :processed
t.timestamps
end
end
end
class AddMatterIdToDiscoveries < ActiveRecord::Migration
def change
add_index :discoveries, :matter_id
add_index :discoveries, :processed
end
end
discoveries_controller.rb
def create
@matter = Matter.find(params[:matter_id])
if(params[:url])
@discovery = Discovery.new
render "new" and return
end
if(params[:discovery][:upload_file_path])
@discovery = Discovery.new(discovery_params)
respond_to do |format|
if @discovery.save
@discovery.matter = current_user.matters.find(params[:matter_id])
format.html { render action: :show, notice: 'Discovery was successfully created.' } # matter_url(@discovery.matter_id)
format.json { render action: 'show', status: :created, location: @discovery }
else
format.html { render action: 'new' }
format.json { render json: @discovery.errors, status: :unprocessable_entity }
end
# redirect_to new_document and return
end
else
@discovery = Discovery.new
render action: 'new', notice: "No file"
end
end
当我在事务模型matters/3/discoveries/new
中创建新发现时,创建了发现,但在控制台中,我认为我应该能够访问Discovery.last.matter
,但我得到错误{{ 1}}
我如何展示发现所属的问题?感谢
答案 0 :(得分:1)
更改模型后,在控制台中调用reload!
(架构更改,运行迁移,添加方法)。