我做了一个简单的关联 - 一个丰富的联合表 - 通过参与来满足用户的需求 和用户通过参与盛宴。 但只有在侧面工作althogh它是完全symeric:
user.rb:
class User < ActiveRecord::Base
require 'digest/sha1'
mount_uploader :image, ImageUploader
has_many :participations
has_many :feasts, :through => :participations
participation.rb:
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :feast
has_many :obligations
has_many :dishes, :through=> :obligations
has_many :groceries, :as => :needed
end
feast.rb:
class Feast < ActiveRecord::Base
mount_uploader :image, ImageUploader
has_many :participations
has_many :users, :through => :participations
has_many :courses
has_many :dishes, :through=>:course
has_many :groceries, :as => :needed
has_many :feast_invt, :as => :invitable
validates_presence_of :feast_time
validates_presence_of :feast_place
end
加载相关数据库:
irb(main):001:0> feast = Feast.find(1)
←[1m←[36mFeast Load (1.0ms)←[0m ←[1mSELECT `feasts`.* FROM `feasts` WHERE `fe
asts`.`id` = ? LIMIT 1←[0m [["id", 1]]
=> #<Feast id: 1, cost: nil, feast_place: "home", feast_time: "2014-03-14 00:00:
00", created_at: "2014-05-19 17:50:30", updated_at: "2014-05-19 17:50:30", image
: nil>
irb(main):002:0> user=User.find(3)
←[1m←[35mUser Load (1.0ms)←[0m SELECT `users`.* FROM `users` WHERE `users`.`i
d` = ? LIMIT 1 [["id", 3]]
=> #<User id: 3, name: "elad bezalel", password: "", email: "
", hashed_password: "", creat
ed_at: "2014-05-14 18:30:46", updated_at: "2014-05-14 18:30:46", shop_cost: nil,
salt: "", city: "", street_num
: "", entrance: "b", level: "3", apartment_num: "7", neighborhood: "
", kosher?: nil, image: "el4.jpg">
irb(main):003:0> par = Participation.find(1)
←[1m←[36m参与负荷(1.0ms)←[0m←[1mSELECT participations
。* FROM p
articipations
WHERE participations
。id
=?限制1←[0m [[&#34; id&#34;,1]]
=> #<Participation id: 1, user_id: 3, feast_id: 1, created_at: "2014-05-19 17:54
:30", updated_at: "2014-05-19 17:54:30", user_costs: nil, meneger?: nil, accepte
d?: nil, coming???: nil>
IRB(主):004:0&GT;
当我写的时候:
users.feasts
它正常工作
但相反的情况并不起作用:( althogh它完全是对称的!)
irb(main):002:0> feast.users
NoMethodError: undefined method `users' for #<ActiveRecord::Relation::ActiveReco
rd_Relation_Feast:0x548cfa0>
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4
.0.2/lib/active_record/relation/delegation.rb:121:in `method_missing'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4
.0.2/lib/active_record/relation/delegation.rb:68:in `method_missing'
from (irb):2
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.2
/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.2
/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.2
/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
我的相关schema.rb:
create_table "feasts", force: true do |t|
t.integer "cost"
t.string "feast_place"
t.datetime "feast_time"
t.datetime "created_at"
t.datetime "updated_at"
t.string "image"
end
create_table "participations", force: true do |t|
t.integer "user_id"
t.integer "feast_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_costs"
t.boolean "meneger?"
t.boolean "accepted?"
t.string "coming???"
end
add_index "participations", ["user_id", "feast_id"], name:
index_participations_on_user_id_and_feast_id", using: :btree
create_table "users", force: true do |t|
t.string "name", limit: 75
t.string "password", limit: 40
t.string "email", default: "", null: false
t.string "hashed_password"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "shop_cost"
t.string "salt"
t.string "city"
t.string "street_num"
t.string "entrance"
t.string "level"
t.string "apartment_num"
t.string "neighborhood"
t.string "kosher?"
t.string "image"
end
end
我甚至试图添加另一个索引
"participations", ["feast_id", "user_id"]
因此它也将是对称的,但它不起作用
怎么做??
答案 0 :(得分:1)
您的feast
变量实际上是ActiveRecord :: Relation,而不是盛宴的单个实例。您只能在盛宴的实际实例上调用users
。
您是如何加载feast
变量的?
答案 1 :(得分:1)
我认为你的问题只是你没有正确定义变量。
这样可以正常使用
user = User.find(1)
user.feasts
feast = Feast.find(1)
feast.users
您有以下内容:
users.feasts
和rails抱怨您尚未定义users
。这是真的,你没有。
你是否犯了错误的想法,当你做
时feast = Feast.first
feast.users
这定义了一个局部变量users
?因为它没有。
你可以做到
feast = Feast.first
users = feast.users
#=> a collection of users
或
feast = Feast.first
user = feast.users.first
user.feasts
#=> get a single user and call .feasts on it
答案 2 :(得分:0)
althogh我在上面的问题中写道==&gt;
feast = feast.find(3)
当问题出现时,这不是我真正写的东西。
我真正做的是==&gt;
feast = Feast.all
因为我在数据库中只有一个盛宴记录,所以我认为这不是问题(所以我甚至没有在问题中提及它并写了另一行作为例子)。
使盛宴成为一个积极的关系对象。所以现在问题得到解决 谢谢你的快速回答。