LINUX.ORG.RU

[rails] Проблема со вложенными has_many :through


0

1
create_table "deliveries", :force => true do |t|
  t.string "name"
  t.text   "description"
end

create_table "shops", :force => true do |t|
  t.string "name"
  t.float  "rating"
end
  
create_table "users", :force => true do |t|
  t.string "name"
  t.string "email"
  t.string "password_digest"
end

create_table "delivery_per_shops", :force => true do |t|
  t.integer "delivery_id"
  t.integer "shop_id"
  t.float   "rating"
end
  
create_table "delivery_per_shops_per_users", :force => true do |t|
  t.integer "rating"
  t.integer "delivery_per_shop_id"
  t.integer "user_id"
end
class Shop < ActiveRecord::Base
  has_many :delivery_per_shops
  has_many :deliveries, :through => :delivery_per_shops
end

class Delivery < ActiveRecord::Base
  has_many :delivery_per_shops
  has_many :shops, :through => :delivery_per_shops
end

class User < ActiveRecord::Base
  has_many :delivery_per_shops_per_users
  has_many :delivery_per_shops, :through => :delivery_per_shops_per_users
end

class DeliveryPerShop < ActiveRecord::Base
  belongs_to :delivery
  belongs_to :shop

  has_many :delivery_per_shops_per_users
  has_many :users, :through => :delivery_per_shops_per_users
end

class DeliveryPerShopsPerUsers < ActiveRecord::Base
  belongs_to :delivery_per_shop
  belongs_to :user
end

Rails 3.1.0. Что тут неверно? Про nested associations читал, но не сильно понял применимо ли это в моём случае.


irb(main):001:0> d = Delivery.first
  Delivery Load (0.2ms)  SELECT `deliveries`.* FROM `deliveries` LIMIT 1
=> #<Delivery id: 1, name: "СПРС", description: "В любые ебеня страны!">
irb(main):002:0> s = Shop.first
  Shop Load (0.1ms)  SELECT `shops`.* FROM `shops` LIMIT 1
=> #<Shop id: 1, name: "Ганджубас-стор ^__^", rating: nil>
irb(main):003:0> ds = s.delivery_per_shops.first
  DeliveryPerShop Load (16.1ms)  SELECT `delivery_per_shops`.* FROM `delivery_per_shops` WHERE `delivery_per_shops`.`shop_id` = 1 LIMIT 1
=> #<DeliveryPerShop id: 1, delivery_id: 1, shop_id: 1, rating: nil>
irb(main):004:0> ds.delivery_per_shops_per_users
NameError: uninitialized constant DeliveryPerShop::DeliveryPerShopsPerUser
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/base.rb:1335:in `compute_type'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/reflection.rb:173:in `klass'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:118:in `klass'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:166:in `find_target?'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:323:in `load_target'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:51:in `load_target'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
	from /var/lib/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
	from /var/lib/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
	from /var/lib/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
	from script/rails:6:in `require'
	from script/rails:6:in `<main>'
irb(main):005:0>
daris
() автор топика
Ответ на: комментарий от daris
irb(main):013:0> dsu = DeliveryPerShopsPerUsers.new(:delivery_per_shop_id => ds.id, :user_id => 1)
=> #<DeliveryPerShopsPerUsers id: nil, rating: nil, delivery_per_shop_id: 1, user_id: 1>
irb(main):014:0> dsu.save
   (8.1ms)  BEGIN
  SQL (19.5ms)  INSERT INTO `delivery_per_shops_per_users` (`delivery_per_shop_id`, `rating`, `user_id`) VALUES (1, NULL, 1)
   (76.3ms)  COMMIT
=> true
irb(main):015:0> ds.delivery_per_shops_per_users
NameError: uninitialized constant DeliveryPerShop::DeliveryPerShopsPerUser
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/base.rb:1335:in `compute_type'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/reflection.rb:173:in `klass'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:118:in `klass'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:166:in `find_target?'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:323:in `load_target'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:51:in `load_target'
	from /var/lib/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
	from /var/lib/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
	from /var/lib/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
	from /var/lib/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
	from script/rails:6:in `require'
	from script/rails:6:in `<main>'
irb(main):016:0>
daris
() автор топика
Ответ на: комментарий от daris
app/models$ sed -i -e "s/delivery_per_shops_per_users/delivery_per_shops_per_users, :class_name => 'DeliveryPerShopsPerUsers'/" *

Решение.

daris
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.