我做了一個簡單的關聯 - 一個豐富的加入表 - 盛宴用戶通過參與 和用戶通過參與盛宴。 但只能在一邊工作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)
←[1米←[36mParticipation負荷(1.0ms的)←[0米←[1mSELECT participations
。* FROM p articipations
WHERE participations
。 id
=? LIMIT 1←[0米[[ 「ID」,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>
事情是,當我寫:
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"]
所以它也將是對稱的,但它沒有工作
該怎麼辦?