一对多关系[Ruby on Rails]

时间:2013-05-03 06:39:16

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

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

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

1 个答案:

答案 0 :(得分:0)

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