[英]rails symetric association works half way
i made a simple association - a rich joined table - feast to user through participation and user to feast through participation. 我做了一个简单的联系 - 一个丰富的联合表 - 通过参与和用户通过参与盛宴给用户盛宴。 but only on side works althogh it is totaly symeric:
但只有在侧面工作althogh它是完全symeric:
user.rb: user.rb:
class User < ActiveRecord::Base
require 'digest/sha1'
mount_uploader :image, ImageUploader
has_many :participations
has_many :feasts, :through => :participations
participation.rb: 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: 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
loading the relevant database: 加载相关数据库:
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←[36mParticipation Load (1.0ms)←[0m ←[1mSELECT participations
.* FROM p articipations
WHERE participations
. ←[1m←[36m参与负荷(1.0ms)←[0m←[1mSELECT
participations
。*从p articipations
WHERE participations
。 id
= ? id
=? LIMIT 1←[0m [["id", 1]] 限制1←[0m [[“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(main):004:0> IRB(主):004:0>
the thing is that when i write: 我写的时候就是:
users.feasts
it works normally 它正常工作
but the opposite just doesn't work: (althogh it is totaly symetric!) 但相反的情况不起作用:( 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>'
my relevant schema.rb: 我的相关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
i even tried to add another index 我甚至试图添加另一个索引
"participations", ["feast_id", "user_id"]
so it will also be symetric but it did not work 所以它也将是对称的,但它没有用
what to do?? 该怎么办??
Your feast
variable is actually an ActiveRecord::Relation, not a single instance of a Feast. 你的
feast
变量实际上是一个ActiveRecord :: Relation,而不是盛宴的单个实例。 You can only call users
on an actual instance of a Feast. 您只能在盛宴的实际实例上呼叫
users
。
How did you load the feast
variable? 你是如何加载
feast
变量的?
I think your problem is just that you're not defining variables properly. 我认为你的问题只是你没有正确定义变量。
This will work fine 这样可以正常工作
user = User.find(1)
user.feasts
feast = Feast.find(1)
feast.users
You have the following: 你有以下几点:
users.feasts
and rails complains that you haven't defined users
. 和rails抱怨你没有定义
users
。 Which is true, you haven't. 哪个是真的,你没有。
Are you perhaps making the mistake of thinking that when you do 你是否犯了错误的想法?
feast = Feast.first
feast.users
this has defined a local variable users
? 这已经定义了一个局部变量
users
? Because it hasn't. 因为它没有。
You could do 你可以做到
feast = Feast.first
users = feast.users
#=> a collection of users
or 要么
feast = Feast.first
user = feast.users.first
user.feasts
#=> get a single user and call .feasts on it
althogh i wrote in my question above ==> althogh我在上面的问题中写道==>
feast = feast.find(3)
that was not what i really wrote when the problem raised. 当问题出现时,这不是我真正写的。 what i really did is ==>
我真正做的是==>
feast = Feast.all
because i had only one feast record in the database, i did not think it is a problem(and so i did not even mention it in the question and wrote another line as an example). 因为我在数据库中只有一个盛宴记录,我不认为这是一个问题(所以我甚至没有在问题中提及它并写了另一行作为例子)。
that made feast to be an active relation object. 使盛宴成为一个积极的关系对象。 so now the problem is fixed thanks for the quick answers.
所以现在问题得到解决,感谢快速解答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.