我有這個模型關係返回false,則不能將產品添加到一個的has_many:通過
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
我預計<<
或concat
方法,當我嘗試添加無法 保存的記錄,像有許多關係返回false ,但似乎與through
關係它不會那樣工作。
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
我傾向於避免直接的數組操作 – apneadiving 2013-02-26 14:33:50