杂种无法添加Embedded_in文档NoMethodError

时间:2018-11-06 17:44:52

标签: ruby-on-rails associations mongoid

我尝试添加文章时遇到问题NoMethodError (undefined method 'articles' for #<Mongoid::Criteria:0x00000007ebefe0>)

我有两个模型:

class Article
  include Mongoid::Document
  field :name, type: String
  field :price, type: BigDecimal

  embedded_in :company, :inverse_of => :articles

end

第二个:

class Company
 include Mongoid::Document
 include Mongoid::Timestamps

 field :name, type: String
 field :user_id, type: Integer

 validates :name, uniqueness: true, presence: true, format: {with: /\A[a-zA-Z0-9 \.]*\z/}

 embeds_many :articles

 validates_associated :articles

 accepts_nested_attributes_for :articles, :reject_if => lambda { |a| a[:title].blank? }, :allow_destroy => true

end

我的控制器看起来像:

def add
     @company = Company.where(user_id: current_user.id)
     @article = @company.articles.create!(params[:article])

if @articles.update
  render :index 
else 
  render :index
end
end

当我尝试添加文章时,我遇到了问题:

  在/ companies / add处的

NoMethodError

     

#Mongoid :: Criteria:0x00000007ebefe0的未定义方法文章

在此先感谢可以帮助我的人

0 个答案:

没有答案