【问题标题】:Rails3 has_many through working on local but fail on herokuRails3 has_many 通过在本地工作但在 heroku 上失败
【发布时间】:2012-10-11 20:36:47
【问题描述】:

我在本地使用 sqlit3,在 heroku 使用 Postgre。

在我将文件上传到 heroku 之前,一切正常。这是我的模型。

class User < ActiveRecord::Base
  belongs_to :unit

  has_friends
end

class Unit < ActiveRecord::Base
  attr_accessible :unit, :floor
  has_many :users
  belongs_to :block
end

class Block < ActiveRecord::Base
  attr_accessible :block, :units_attributes
  has_many :units, :dependent => :destroy
  accepts_nested_attributes_for :units, allow_destroy: true
  belongs_to :postalcode
end

class Postalcode < ActiveRecord::Base
  attr_accessible :postalcode, :blocks_attributes
  has_many :blocks, :dependent => :destroy
  accepts_nested_attributes_for :blocks, allow_destroy: true
  belongs_to :neighbourhood
end

class Neighbourhood < ActiveRecord::Base
  attr_accessible :name, :streetname, :postalcodes_attributes
  has_many :postalcodes, :dependent => :destroy
  has_many :blocks, :through => :postalcodes
  has_many :units, :through => :blocks
  has_many :users, :through => :units
  accepts_nested_attributes_for :postalcodes, allow_destroy: true
  validates :name, :presence => true
  validates :streetname, :presence => true
end

我排查了一下,发现问题出在控制器这个方法上。

@neighbours = current_user.unit.block.postalcode.neighbourhood.users

虽然@neighbours = current_user.unit.block.postalcode.neighbourhood 工作得很好。

请帮忙,我很绝望,我已经尝试了一整天的谷歌搜索。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    查看this answer to a similar issue

    错误很可能来自WHERE "postalcodes"."neighbourhood_id" = 1,这表明邮政编码表中的neighbourhood_id 是作为字符串创建的,而不是整数。

    相应地按照答案中提到的步骤,并将其更改为整数。

    【讨论】:

    • 几乎是对的,但是是的。这是我的用户模型 unit_id,很奇怪。但是谢谢,我爱你:D
    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 2011-07-31
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2018-04-15
    相关资源
    最近更新 更多