Rails ActiveRecord 查询抛出未定义的 BelongsToAssociation 错误

发布于 2024-10-25 16:09:35 字数 1659 浏览 0 评论 0原文

我有一个 Rails 2.3 模型出版物,其中包含 own_to 关联版本。当我运行此 activerecord 时,发现它第一次工作,但刷新页面时出现此错误 undefined methodversion' for #`。

Publication.find(:all, :limit => 10, :order => "date_published desc", 
    :include => [:version => {:work => :work_type}, 
                 :products => {:media_type => :meaning} ])

class Publication < ActiveRecord::Base
  acts_as_nested_set

  belongs_to :imprint
  has_many :products
  has_many :subscriptions
  belongs_to :cover_image
  belongs_to :version

  accepts_nested_attributes_for :products, :allow_destroy => true
  accepts_nested_attributes_for :subscriptions, :allow_destroy => true
  accepts_nested_attributes_for :cover_image, :allow_destroy => true
  accepts_nested_attributes_for :version
end



class Version < ActiveRecord::Base
  belongs_to :work

  has_many(:authorships, :as => :authorable, :include => [:role])
  has_many(:user_authorships, :as => :authorable, 
            :conditions => { :authorizable_type => "User" })
  has_many(:organization_authorships, :as => :authorable, 
            :conditions => { :authorizable_type => "Organization" })

  has_one :element, :class_name => "Element", :foreign_key => :version_id

  has_many :tag_links, :as => :taggable, :class_name => 'TagLink'
  has_many :tags, :through => :tag_links, :class_name => 'Tag'

  accepts_nested_attributes_for :authorships, :allow_destroy => true
  accepts_nested_attributes_for :work
end

I have the a Rails 2.3 model Publication that contains the belongs_to association version. When I run this activerecord find it works the first time but refreshing the page give me this error undefined methodversion' for #<ActiveRecord::Associations::BelongsToAssociation:0x10cb2a338>`.

Publication.find(:all, :limit => 10, :order => "date_published desc", 
    :include => [:version => {:work => :work_type}, 
                 :products => {:media_type => :meaning} ])

class Publication < ActiveRecord::Base
  acts_as_nested_set

  belongs_to :imprint
  has_many :products
  has_many :subscriptions
  belongs_to :cover_image
  belongs_to :version

  accepts_nested_attributes_for :products, :allow_destroy => true
  accepts_nested_attributes_for :subscriptions, :allow_destroy => true
  accepts_nested_attributes_for :cover_image, :allow_destroy => true
  accepts_nested_attributes_for :version
end



class Version < ActiveRecord::Base
  belongs_to :work

  has_many(:authorships, :as => :authorable, :include => [:role])
  has_many(:user_authorships, :as => :authorable, 
            :conditions => { :authorizable_type => "User" })
  has_many(:organization_authorships, :as => :authorable, 
            :conditions => { :authorizable_type => "Organization" })

  has_one :element, :class_name => "Element", :foreign_key => :version_id

  has_many :tag_links, :as => :taggable, :class_name => 'TagLink'
  has_many :tags, :through => :tag_links, :class_name => 'Tag'

  accepts_nested_attributes_for :authorships, :allow_destroy => true
  accepts_nested_attributes_for :work
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文