简体   繁体   English

Rails 4:无法创建与此模型关联的模型的新实例

[英]Rails 4: cannot create a new instance of model associated with this model

I have a Rails App which has a Model called "Service" which has an association with another model called "Upload". 我有一个Rails应用程序,该应用程序具有一个名为“ Service”的模型,该模型与另一个名为“ Upload”的模型有关联。 A Service has_many Uploads and an Upload belongs_to Service. 服务has_many上载,并且上载belongs_to服务。 Uploads is a model that is using the paperclip gem to model an uploaded file. 上载是使用回形针gem为上载文件建模的模型。

Here are the Model classes for both: 这是两者的模型类:


service.rb: service.rb:

         # == Schema Information
         #
         # Table name: services
         #
         #  id            :integer          not null, primary key
         #  created_at    :datetime
         #  updated_at    :datetime
         #  last_run_time :datetime
         #  name          :string(255)
         #  description   :text
         #
  class Service < ActiveRecord::Base
       attr_accessor :name, :description
       has_many :uploads
     end

upload.rb upload.rb

       # == Schema Information
       #
       # Table name: uploads
       #
       #  id                      :integer          not null, primary key
       #  created_at              :datetime
       #  updated_at              :datetime
       #  sourcedata_file_name    :string(255)
       #  sourcedata_content_type :string(255)
       #  sourcedata_file_size    :integer
       #  sourcedata_updated_at   :datetime
       #  service_id              :integer
       #

     class Upload < ActiveRecord::Base

      has_attached_file :sourcedata
      belongs_to :service

      end

My constructor methods using rails associations dont seem to be working. 我的使用Rails关联的构造方法似乎不起作用。

When I run this at rails console I see the following: 当我在Rails控制台上运行此命令时,我看到以下内容:

s = Service.new s = Service.new

 => #<Service id: nil, created_at: nil, updated_at: nil, last_run_time: nil, name: nil, description: nil>


  >> > @u = s.upload.new

  NoMethodError: undefined method `upload' for #<Service:0x007fc4c7818470>

  >>@u = s.create_upload()

  NoMethodError: undefined method `create_upload' for #<Service:0x007fc4c7818470>

 >>@u = s.uploads.build

  NoMethodError: undefined method `uploads' for #<Service:0x007fc4c7818470>

 >> @u = s.uploads.create

 NoMethodError: undefined method `uploads' for #<Service:0x007fc4c7818470>

I tried to create the associated model instance using these methods and it doesn't seem to be working. 我试图使用这些方法创建关联的模型实例,但它似乎不起作用。 Im wondering what am I doing wrong. 我想知道我在做什么错。 Could someone help me please. 有人可以帮我吗。

Thanks 谢谢


s.inspect = s.inspect =

   => "#<Service id: 6, created_at: \"2013-11-27 16:41:43\", updated_at: \"2013-11-27 16:41:43\", 
   last_run_time: nil, name: nil, description: nil>"

Try following: 请尝试以下操作:

s.uploads.build 
s.uploads.create

You are using has_many , so the upload method does not exist because you have many of them... 您正在使用has_many ,因此上upload方法不存在,因为其中有很多...

Use s.uploads.new (or s.uploads.build ) or s.uploads.create and read the ActiveRecord documentation. 使用s.uploads.new (或s.uploads.build )或s.uploads.create并阅读ActiveRecord文档。

You want: 你要:

s.uploads.build

and

s.uploads.create

You can read about what methods are available here: 您可以在此处了解可用的方法:

http://guides.rubyonrails.org/association_basics.html#has-many-association-reference http://guides.rubyonrails.org/association_basics.html#has-many-association-reference

4.3.1 Methods Added by has_many 4.3.1 has_many添加的方法

When you declare a has_many association, the declaring class automatically gains 13 methods related to the association: 声明has_many关联时,声明类自动获得13种与该关联相关的方法:

collection(force_reload = false)
collection<<(object, ...)
collection.delete(object, ...)
collection.destroy(object, ...)
collection=objects
collection_singular_ids
collection_singular_ids=ids
collection.clear
collection.empty?
collection.size
collection.find(...)
collection.where(...)
collection.exists?(...)
collection.build(attributes = {}, ...)
collection.create(attributes = {})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM