Ruby on rails 为什么我的多对多关联失败了?
我已经建立了一个类似twitter的跟踪模型。用户可以相互订阅。尝试创建关系时,我的用户控制器中出现错误 user.rb:Ruby on rails 为什么我的多对多关联失败了?,ruby-on-rails,ruby-on-rails-3,model-associations,Ruby On Rails,Ruby On Rails 3,Model Associations,我已经建立了一个类似twitter的跟踪模型。用户可以相互订阅。尝试创建关系时,我的用户控制器中出现错误 user.rb: has_many :subscriptions has_many :providers, :through => :subscriptions has_many :followings, :class_name => "Subscription" has_many :followers, :through => :followings def foll
has_many :subscriptions
has_many :providers, :through => :subscriptions
has_many :followings, :class_name => "Subscription"
has_many :followers, :through => :followings
def follow(other_user)
Subscription.create(:provider => other_user, :follower => self)
end
has_many :subscriptions, :foreign_key => "subscriber_id", :dependent => :destroy
has_many :subscribed_to, :through => :subscriptions, :source => :provider
has_many :reverse_subscriptions, :class_name => "Subscription", :foreign_key => "provider_id", :dependent => :destroy
has_many :followers, :through => :reverse_subscriptions
def following?(provider)
subscriptions.find_by_provider_id(provider)
end
def follow!(provider)
subscriptions.create!(:provider_id => provider.id)
end
def unfollow!(provider)
subscriptions.find_by_provider_id(provider).destroy
end
subscription.rb
belongs_to :provider, :class_name => 'User', :foreign_key => "provider_id"
belongs_to :follower, :class_name => 'User', :foreign_key => "follower_id"
用户\u 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
def follow
@user = User.find(params["user_id"])
if current_user.follow(@user)
flash[:notice] = "Subscribed"
else
flash[:error] = "Unable to subscribe."
end
end
我已经验证我运行了rake db:migrate-订阅表有两个字段provider\u id和follower\u id。有人能帮我解决这个错误并解释它为什么要查找“user\u id”属性吗
更新:
show.html.erb:
<%= button_to "Subscribe", user_follow_path(@user), :remote => true %>
rake路线| grep如下:
user_follow POST /users/:user_id/follow(.:format) {:action=>"follow", :controller=>"users"}
它给您一个错误的原因是,当您调用
作为指南时,我提出了这个解决方案,它修复了数据模型,使收集函数能够正常工作
使提供程序id可访问并删除订阅模型中的外键
subscription.rb:
attr_accessible :provider_id
belongs_to :provider, :class_name => 'User'
belongs_to :follower, :class_name => 'User'
在用户模型中为订阅和反向订阅添加外键
user.rb:
has_many :subscriptions
has_many :providers, :through => :subscriptions
has_many :followings, :class_name => "Subscription"
has_many :followers, :through => :followings
def follow(other_user)
Subscription.create(:provider => other_user, :follower => self)
end
has_many :subscriptions, :foreign_key => "subscriber_id", :dependent => :destroy
has_many :subscribed_to, :through => :subscriptions, :source => :provider
has_many :reverse_subscriptions, :class_name => "Subscription", :foreign_key => "provider_id", :dependent => :destroy
has_many :followers, :through => :reverse_subscriptions
def following?(provider)
subscriptions.find_by_provider_id(provider)
end
def follow!(provider)
subscriptions.create!(:provider_id => provider.id)
end
def unfollow!(provider)
subscriptions.find_by_provider_id(provider).destroy
end
我还向用户模型添加了助手方法
user.rb:
has_many :subscriptions
has_many :providers, :through => :subscriptions
has_many :followings, :class_name => "Subscription"
has_many :followers, :through => :followings
def follow(other_user)
Subscription.create(:provider => other_user, :follower => self)
end
has_many :subscriptions, :foreign_key => "subscriber_id", :dependent => :destroy
has_many :subscribed_to, :through => :subscriptions, :source => :provider
has_many :reverse_subscriptions, :class_name => "Subscription", :foreign_key => "provider_id", :dependent => :destroy
has_many :followers, :through => :reverse_subscriptions
def following?(provider)
subscriptions.find_by_provider_id(provider)
end
def follow!(provider)
subscriptions.create!(:provider_id => provider.id)
end
def unfollow!(provider)
subscriptions.find_by_provider_id(provider).destroy
end
然后,在控制器中,我们可以调用follow!否则就放弃
user_controller.rb:
...
current_user.unfollow!(@user)
...
current_user.follow!(@user)
...
我正在使用Desive,因此我在\u筛选器之前有以下内容:在\u筛选器之前:验证\u用户!,:only=>[:update,:follow]
好的,我想我可能知道发生了什么,我会更新我的答案。对不起,我又编辑了一次。还添加了关于
反向原因的链接。如果可以的话,让我知道我不知道它如何与自引用关联一起工作。为了逻辑起见,我做了一些编辑和抽象。但从Rails3.1开始,只要设置了:inverse\u of
选项,就可以通过关联操作一些。我认为你的问题在于它是自我参照的。幸运的是,当使用此解决方法时,模型仍然存在问题(例如,查找不工作)。我发布了一个更完整的解决方案来修复模型。你能在代码中注释users\u controller.b的第73行吗?