Rails - 檢查 has_many 關聯中是否存在記錄 (Rails - check if record exists in has_many association)


問題描述

Rails ‑ 檢查 has_many 關聯中是否存在記錄 (Rails ‑ check if record exists in has_many association)

我不確定我的問題措辭是否正確。

我有三個模型:UserItemUserItem .

user has_many :user_items
user has_many :items, through :user_items

item has_many :user_items
item has_many :users ‑> {uniq}, through :user_items
item belongs_to :user

user_item belongs_to :user
user_item belongs_to :item

我需要一種方法來查看用戶是否有一個項目來在我的項目視圖中創建 if 語句但這裡有一個問題,user_items 有 枚舉狀態:[:pending,approved]。所以我需要查看 current_user 是否有某個 :pending 項目。

例如,當用戶訪問 item1 的查看頁面時,我有 item_controller 的顯示動作聲明 @item = Item.find_by_id(params[:id])。但是我可以用這個 @item 做什麼來查看用戶是否有這個項目?


參考解法

方法 1:

Try:

current_user.items.exists?(params[:id])

Or

current_user.items.exists?(@item.id)

方法 2:

Extending @lei‑liu's answer here. One can find if the record exists among the many or not, through: current_user.items.exists?(params[:id])

At the same time, exists? allows one to filter through the columns besides id, and also allows for more complicated conditions, like the following:

current_user.items.exists?('id > 3')
current_user.items.exists?(name: 'some_name')

方法 3:

But then what can I do with this @item to see if a user has this item?

I think what you are missing here is model methods. For example, if you added a method to the Item model called belongs_to_user_in_pending_state, you'd be able to call @item.belongs_to_user_in_pending_state(current_user) anywhere you need it.

def belongs_to_user_in_pending_state(user)
  if self.user_items.pending.select {|s| s.user == user}.count > 0 
    return true
  else
    return false
  end
end

(by user4584963lei liuArslan Alitkz79)

參考文件

  1. Rails ‑ check if record exists in has_many association (CC BY‑SA 2.5/3.0/4.0)

#has-many #ActiveRecord #ruby-on-rails #associations






相關問題

Ruby on Rails - Has_many,批量分配問題 (Ruby on Rails - Has_many, Mass Assignment issue)

如何在 hasMany 關聯中使用 CakePHP 2 查找? (How to use CakePHP 2 find in a hasMany association?)

模型沒有像我認為的那樣連接 (Models not connecting as I think they should be)

CakePhp find không tìm nạp hasMany mối quan hệ (CakePhp find doesn't fetch hasMany relationships)

創建關聯後,控制器中的 Ruby on rails NoMethodError (Ruby on rails NoMethodError in controller after create an association)

是否可以以兩種不同的方式關聯模型? (Is it possible to associate models in two different ways?)

在 DetailView yii2 中顯示 HAS_MANY 關係數據 (Display HAS_MANY relation data in DetailView yii2)

Rails - 檢查 has_many 關聯中是否存在記錄 (Rails - check if record exists in has_many association)

在 Rails 中,我可以在另一個模型的模型上設置 has_many 關係嗎? (In Rails, can I set a has_many relation on a model from another model?)

從大於 0 開始計數器緩存 (Starting a counter cache from greater than 0)

如何使用 has_many 關聯在 Rails 中排序? (How to sort in rails with has_many association?)

如何將 ActiveModel::Serializer :has_many 關聯的密鑰格式(即駝峰式)指定為一次性選項(不是全局配置)? (How to specify the key format (i.e. camelcase) for an ActiveModel::Serializer :has_many association as a one-off option (not global config)?)







留言討論