【问题标题】:namespaced model + postgresql issue命名空间模型 + postgresql 问题
【发布时间】:2013-11-15 16:50:42
【问题描述】:

对 PostgreSQL 和我不理解的命名空间模型有疑问。

@photos = Profile::Photo.where(:attachable_id => id)

这会抛出一个:

 !! #<ActiveRecord::StatementInvalid: PG::UndefinedTable: 
  ERROR:  relation "profile_photos" does not exist
 LINE 1: SELECT "profile_photos".* FROM "profile_photos"  WHERE "prof...
                                        ^
 : SELECT "profile_photos".* FROM "profile_photos"  
 WHERE "profile_photos"."attachable_id" = 1>

我有一个模型 app/models/profile/photo.rb,例如:

class Profile::Photo < ActiveRecord::Base
  mount_uploader :file, PhotoUploader
  belongs_to :attachable, :polymorphic => true   belongs_to :user,
  :dependent => :destroy   #belongs_to  :profile, :foreign_key =>
  'attachable_id', :dependent => :destroy

它说关系不正确,但这在命名模型之前完美运行。我在这里忽略了什么?

【问题讨论】:

  • 数据库中的表的名称是什么?你之前提到它工作得很好,如果不是,你可能需要使用set_table_name,实际上,profile_photos
  • 我的表好像重命名为profiles_photos,我认为它必须是多重的,现在不确定

标签: ruby-on-rails postgresql activerecord postgresql-9.3


【解决方案1】:

问题是不正确的命名模型,我重构以更正复数格式并正确重命名表,从而解决了问题。

【讨论】:

    猜你喜欢
    • 2012-02-16
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 2020-02-25
    • 2011-02-05
    • 2012-09-10
    • 1970-01-01
    相关资源
    最近更新 更多