【问题标题】:Unable to find rails association Model找不到 Rails 关联模型
【发布时间】:2015-09-06 05:39:38
【问题描述】:

我正在尝试通过联结表进行查询,尽管 Rails 给了我以下错误

场地模型

class Venue < ActiveRecord::Base


attr_accessible :address, :latitude, :longitude, :name, :phone, :suburb, :state, :country
  after_validation :geocode

  has_many :orders, through: :venues_orders

  geocoded_by :full_address


  def full_address
    [address, suburb, state, country].compact.join(', ')
  end

end

订单模式

class Order < ActiveRecord::Base
  attr_accessible :fulfilled, :item, :placed, :person_id, :special_instructions, :priority, :flag, :milk

  belongs_to :person
  belongs_to :venue

场地订单模型

class VenuesOrders < ActiveRecord::Base
  attr_accessible :order_id, :venue_id
end

【问题讨论】:

  • 你的关联不正确

标签: ruby-on-rails postgresql activerecord model-associations


【解决方案1】:
class Venue < ActiveRecord::Base

  has_many :orders, through: :venues_orders
  has_many :venues_orders   

end

class Order < ActiveRecord::Base

  has_many :venues, through: :venues_orders
  has_many :venues_orders   

end

class VenuesOrders < ActiveRecord::Base
  belongs_to :venue
  belongs_to :order
end

更多详情请阅读:RailsGuides

另一个建议:创建join table 的约定是词法排序。比如OrdersVenues 不是VenuesOrders

【讨论】:

  • 谢谢,我已经回滚了模型并重新创建以使用词法排序
  • 仍然收到同样的警告信息
猜你喜欢
  • 2022-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多