【问题标题】:Ruby on Rails || Following conferencesRuby on Rails ||以下会议
【发布时间】:2017-04-30 14:57:58
【问题描述】:

我正在尝试在 Ruby on Rails 中创建一个应用程序,我可以:

  • 创建会议(工作)

  • 查看会议列表(工作中)

  • 关注和取消关注会议(无效)

  • 查看以下会议(不工作)

我早在 12 月就开始使用 Ruby on Rails,并使用 Micheal Hartl 的教程开始使用。在第 14 章 (https://www.railstutorial.org/book/following_users) 中,Micheal 介绍了关注和取消关注用户、低谷关系。

我正在尝试应用他的技术,但将其调整到一个模型和一个控制器之间存在关系的地方,以及有两个模型和两个控制器的地方。

一个控制器和模型是用户控制器和用户模型,另一个控制器和模型是会议控制器和会议模型。

我首先向用户模型添加活动关系,因为它是关注会议的一方

user.rb

  has_many :active_relationships, class_name:  "Relationship",
                                  foreign_key: "follower_id",
                                  dependent:   :destroy
  has_many :following, through: :active_relationships, source: :followed

我在会议模式中做了相反的事情,因为我是被关注的一方

Conference.rb

has_many :passive_relationships, class_name:  "Relationship",
                                   foreign_key: "followed_id",
                                   dependent:   :destroy
  has_many :followers, through: :passive_relationships, source: :follower

为了清除结构,我在关系模型中添加了以下代码行

Relationship.rb

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "Conference"
  validates :follower_id, presence: true
  validates :followed_id, presence: true

当试图查看用户是否真的在关注会议时,用户模型中出现错误:

ActiveRecord::RecordNotUnique in RelationshipsController#create

SQLite3::ConstraintException: UNIQUE constraint failed: relationships.follower_id, relationships.followed_id: INSERT INTO "relationships" ("follower_id", "followed_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)

问题的根源在于:

app/models/user.rb:116:in `follow'
app/controllers/relationships_controller.rb:6:in `create'

我知道当记录无法插入时会出现问题,因为它会违反唯一性约束,但我不知道违反了什么唯一性约束。

现在第一个问题发生在 user.rb 中,此时正在用户和会议之间创建 active_relationship。

  # Returns true if the current user is following the other user.
  def following?(other_conference)
    following.include?(other_user)
  end

  # Follows a conference.
  def follow(other_conference)
    active_relationships.create(followed_id: other_conference.id)
  end

  # Unfollows a conference.
  def unfollow(other_conference)
    active_relationships.find_by(followed_id: other_conference.id).destroy
  end

第二个问题在Relationships_controller 中,current_user 应该关注会议。

def create
    @conference = Conference.find(params[:followed_id])
    current_user.follow(@conference)
    respond_to do |format|
      format.html { redirect_to @conference }
      format.js
    end
  end

现在我不确定问题的原因是什么以及如何解决它。我希望我已经明确了我的问题以及我想要实现的目标。如果不是,我很乐意提供有关我的问题的更多信息。

【问题讨论】:

  • has_many :followings - 在 Rails 中创建关联和命名事物时要特别注意复数。

标签: ruby-on-rails ruby relationship


【解决方案1】:

您正在关注一个处理更复杂情况的示例(您要加入同一个表两次)并且您的解决方案比它需要的复杂一点:

class User
  has_many :subscriptions
  has_many :conferances, though: :subscriptions

  def subscribed_to?(conference)
    conferances.include?(conference)
  end

  def find_subscription(conference)
    subscriptions.find_by(conference: conference)
  end
end

class Conferance
  has_many :subscriptions
  has_many :users, though: :subscriptions
end

# Its better to name join models after an actual thing
class Subscription
  belongs_to :user
  belongs_to :conference
end

resources :conferences, shallow: true do
  resource :subscriptions, only: [:create, :destroy]
end

class SubscriptionsController
  before_action :set_conferance, only: :create

  def create
    if current_user.subsciptions.create(conferance: @conferance)
      flash[:success] = "You are now subscribed to { @conferance.name }"
    else
      flash[:error] = "Could not create subscription."
    end
    redirect_to @conferance
  end

  def destroy
    @subscription = current_user.subsciptions.find(params[:id])
    if @subscription.destroy
      flash[:success] = "You are no longer subscribed to { @conferance.name }"
    else
      flash[:error] = "Oh noes"
    end

    redirect_to @subscription.conferance 
  end

  def set_conferance
    @conferance = Conferance.find(params[:conferance_id])
  end
end

<% if current_user.subscribed_to?(@conferance) %>
  <%= button_to "Subscribe", conferance_subsciptions_path(@conferance), method: :post %>
<% else %>
  <%= button_to "Unsubscribe", subscription_path(current_user.find_subscription(@conferance)), method: :delete %>
<% end %>

【讨论】:

  • 我用 Subscription 代替了,因为将 follow 弯曲成名词并且各种形式都很尴尬。
  • 谢谢 Max,这帮助我解决了我的问题!我希望我有办法报答你!
  • Max,我遇到了 def set_conferance 的问题,我收到错误消息 undefined local variable or method `conference_id'
  • nvm,明白了! @conference = Conference.find_by id: params["conference_id"] :)
  • 糟糕,很高兴你发现了我的错字。
猜你喜欢
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多