2

我有这个模型关系

class User < ActiveRecord::Base
  has_many :trip_memberships, dependent: :destroy
  has_many :trips, through: :trip_memberships, uniq: true
end

class Trip < ActiveRecord::Base
  has_many :trip_memberships, dependent: :destroy
  has_many :members, through: :trip_memberships, source: :user, uniq: true
end

class TripMembership < ActiveRecord::Base
  belongs_to :trip
  belongs_to :user
end

当我将一些用户添加为旅行成员时,例如以下代码:

trip = Trip.last
john = User.last
trip.members # => []  Empty right now
trip.members << john
trip.members # => [john]  Contains John
trip.members << john
trip.members # => [john]  Contains only John, but...
TripMembership.all # => [TripMembership(trip, john), TripMembership(trip, john)]
                   # There is 2 memberships, even the accessor methods only show one
                   # member because of the :uniq option

我不想在trip_memberships表格中有重复,但我想遵循“告诉,不要问”的原则。我不想检查给定记录是否存在。

我将此验证添加到 TripMembership 模型中

class TripMembership < ActiveRecord::Base
  belongs_to :trip
  belongs_to :user
  validates_presence_of :trip_id, :user_id
  validates_uniqueness_of :trip_id, scope: [:user_id]
  validates_uniqueness_of :user_id, scope: [:trip_id]
end

当我尝试添加无法保存的记录时,我希望<<or方法返回 false ,就像在有很多关系中一样,但似乎关系不是那样工作的。concatthrough

trip.members << john # => ActiveRecord::RecordInvalid: Validation failed: Trip has already been taken, User has already been taken

任何人都知道一种优雅的归档方式吗?我认为应该这样使用api。

if @trip.members << new_member
   flash[:success] = "new member added"
else
   flash[:error] = "can't add this member to trip"
end
4

2 回答 2

3

你可以这样尝试:

 if @trip.members.include?(new_member)
    flash[:error] = "Member already exists"
 else
    @trip.members << new_member
    flash[:success] = "new member added"
 end

编辑

has_many :members, through: :trip_memberships, source: :user do
  def <<(member)
    if self.include?(member)
      false
    else
      super(Array(member)-self)
    end
  end
end
于 2013-02-26T14:29:54.267 回答
1

我刚刚有了一个想法。

由于我刚刚发现 Rails 的标准行为是在用户不是新记录时引发异常,并且我不能更改知名方法的默认行为,所以我将使用自定义扩展我的关联在开始/救援中调用的方法add_member只是别名调用。<<

has_many :members, through: :trip_memberships, source: :user, uniq: true do
  def add(*records)
    self.<< records
  rescue ActiveRecord::RecordInvalid
    false
  end
end

我有自己的方法,可以按预期工作,而无需更改 rails 方法。

于 2013-02-26T14:45:48.487 回答