【问题标题】:One-to-Many Relation [Ruby on Rails]一对多关系 [Ruby on Rails]
【发布时间】:2013-05-03 06:39:16
【问题描述】:

我不知道财产和所有者之间的关系是否有效,当我尝试查询时出现此错误:

Loading development environment (Rails 3.2.13)
irb(main):001:0> Owner.find(1).properties
  Owner Load (18.1ms)  SELECT "owners".* FROM "owners" WHERE "owners"."id" = ? LIMIT 1  [["id", 1]]
  Property Load (0.1ms)  SELECT "properties".* FROM "properties" WHERE "properties"."owner_id" = 1
ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'House'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Property.inheritance_column to use another column for that information.
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/inheritance.rb:143:in `rescue in find_sti_class'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/inheritance.rb:136:in `find_sti_class'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/inheritance.rb:62:in `instantiate'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/querying.rb:38:in `block (2 levels) in find_by_sql'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/querying.rb:38:in `collect!'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/querying.rb:38:in `block in find_by_sql'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/explain.rb:41:in `logging_query_plan'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/querying.rb:37:in `find_by_sql'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/relation.rb:171:in `exec_queries'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/relation.rb:160:in `block in to_a'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/explain.rb:34:in `logging_query_plan'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/relation.rb:159:in `to_a'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:159:in `all'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:382:in `find_target'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:335:in `load_target'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
    from /var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
    from /var/lib/gems/1.9.2/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /var/lib/gems/1.9.2/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /var/lib/gems/1.9.2/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'

我的 property.rb 文件:

class Property < ActiveRecord::Base
  attr_accessible :owner_id, :p_city, :p_street, :postcode, :rent, :rooms, :type
  belongs_to :owner
  has_one :ticket
end

我的 owner.rb 文件:

class Owner < ActiveRecord::Base
  attr_accessible :address, :f_name, :l_name, :tel_no
  has_many :properties
end

【问题讨论】:

    标签: ruby-on-rails database ruby-on-rails-3.2 model-associations


    【解决方案1】:

    您不能将“类型”用作属性/列,因为它是为继承的东西保留的。您应该将模型的属性和 db 列都更改为其他内容。

    【讨论】:

    • 你的意思是我应该重命名那个叫做'type'的属性?
    • 是的,您必须将其更改为其他内容,例如“property_type”。
    • 是的。您不能使用“type”作为列名,因为活动记录使用它来存储 ruby​​ 类名(用于单表继承)。参考链接:ruby-forum.com/topic/101557
    • 感谢 grotori 和 NikDP,我会试试的,抱歉我太菜鸟 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多