为什么我的多对多关联失败?
我建立了一个类似推特的关注模型。用户都可以互相订阅。尝试创建关系时,我的用户控制器中出现错误。
user.rb:
has_many :subscriptions
has_many :providers, :through => :subscriptions
has_many :followings, :class_name => "Subscription"
has_many :followers, :through => :followings
subscription.rb
belongs_to :provider, :class_name => 'User', :foreign_key => "provider_id"
belongs_to :follower, :class_name => 'User', :foreign_key => "follower_id"
users_controller.rb
69 def follow
70 logger.debug params.to_yaml
71 @user = User.find(params["user_id"])
72 logger.debug @user.to_yaml
73 if current_user.providers << @user
74 flash[:notice] = "Subscribed"
75 else
76 flash[:error] = "Unable to subscribe."
77 end
78 end
这是我调用follow时出现的错误:
ActiveRecord::UnknownAttributeError (unknown attribute: user_id):
app/controllers/users_controller.rb:73:in `follow'
我已经验证我运行了rake db:migrate - 订阅表有两个字段provider_id和follower_id。谁能帮我解决这个错误并解释为什么它正在寻找“user_id”属性?
更新:
show.html.erb:
<%= button_to "Subscribe", user_follow_path(@user), :remote => true %>
routes.rb:
resources :users do
resources :locations
resources :saved_events
resources :saved_locations
post "follow"
end
rake 路线 | grep 遵循:
user_follow POST /users/:user_id/follow(.:format) {:action=>"follow", :controller=>"users"}
I have set up a twitter-like following model. Users can all subscribe to each other. I am getting an error in my users controller when trying to create the relationship.
user.rb:
has_many :subscriptions
has_many :providers, :through => :subscriptions
has_many :followings, :class_name => "Subscription"
has_many :followers, :through => :followings
subscription.rb
belongs_to :provider, :class_name => 'User', :foreign_key => "provider_id"
belongs_to :follower, :class_name => 'User', :foreign_key => "follower_id"
users_controller.rb
69 def follow
70 logger.debug params.to_yaml
71 @user = User.find(params["user_id"])
72 logger.debug @user.to_yaml
73 if current_user.providers << @user
74 flash[:notice] = "Subscribed"
75 else
76 flash[:error] = "Unable to subscribe."
77 end
78 end
This is the error when I call follow:
ActiveRecord::UnknownAttributeError (unknown attribute: user_id):
app/controllers/users_controller.rb:73:in `follow'
I have verified that I ran rake db:migrate - the subscription table has two fields provider_id and follower_id. Can anyone help me with the error and explain why it is looking for a 'user_id' attribute?
Update:
show.html.erb:
<%= button_to "Subscribe", user_follow_path(@user), :remote => true %>
routes.rb:
resources :users do
resources :locations
resources :saved_events
resources :saved_locations
post "follow"
end
rake routes | grep follow:
user_follow POST /users/:user_id/follow(.:format) {:action=>"follow", :controller=>"users"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Michael Hartl 的教程 作为指导,我想出了这个解决方案,它修复了数据模型,以便收集功能按其应有的方式工作。
使提供者 ID 可访问并删除订阅模型中的外键。
subscription.rb:
在用户模型中添加订阅和reverse_subscriptions的外键。
user.rb:
我还在用户模型中添加了辅助方法。
user.rb:
然后,在控制器中,我们可以调用follow!或取消关注!
用户控制器.rb:
Using Michael Hartl's tutorial as a guide, I came up with this solution, which fixes the data model so that collection functions work as they should.
Make the provider id accessible and remove the foreign keys in the subscription model.
subscription.rb:
Add foreign keys for subscriptions and reverse_subscriptions in the user model.
user.rb:
I also added helper methods to the user model.
user.rb:
Then, in the controller, we can call follow! or unfollow!
user_controller.rb:
它给你一个错误的原因是因为当你在提供者集合上调用
<<
时,用户不知道它应该是一个追随者。所以它基本上是在说:“我是用户,请将我添加到这个提供商集合中!”而不是“这个人要追随我,我现在是提供者,他是追随者”最简单的答案可能是只执行
user.rb:
users_controller.rb
The reason it's giving you a error is because the when you call
<<
on the providers collection the user doesn't know it's supposed to be a follower. So it's basically saying, "I'm a user, add me to this collection of providers!" instead of "This guy is going to follow me, I'm now a provider and he's a follower"The simplest answer may be to just do
user.rb:
users_controller.rb