Search code examples
ruby-on-railsdatabaseruby-on-rails-3.2model-associations

One-to-Many Relation [Ruby on Rails]


I don't know if the relation between property and owner working or not , when I tried a query I got this error:

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'

my property.rb file:

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

my owner.rb file:

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

Solution

  • You can't use "type" as an attribute/column because it is reserved for inheritance stuff. You should change both your model's attribute and the db column to something else.