2014-05-20 41 views
0

我做了一個簡單的關聯 - 一個豐富的加入表 - 盛宴用戶通過參與 和用戶通過參與盛宴。 但只能在一邊工作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 participationsid =? 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"] 

所以它也將是對稱的,但它沒有工作

該怎麼辦?

回答

1

您的feast變量實際上是一個ActiveRecord :: Relation,而不是一個Feast的單個實例。您只能在宴會的實際實例上致電users

你是如何加載feast變量的?

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 
0

althogh我在上面==我的問題寫>

feast = feast.find(3) 

,這不是我真正的問題時提出的寫。 我真的是==>

feast = Feast.all 

,因爲我已經在數據庫中,我不認爲這是一個問題(因此我甚至沒有提到它的問題只有一個盛宴記錄,並又寫了線作爲例子)。

使盛宴成爲一個活躍的關係對象。所以現在問題修復了 感謝您的快速解答。