我有以下型號:確保陣列中的至少一個項目存在於另一個
class Post < ActiveRecord::Base
has_and_belongs_to_many :countries
end
class User < ActiveRecord::Base
has_many :entitlements
has_many :countries, :through => :entitlements
end
郵政索引頁帖子必須至少有一個國家是一樣的用戶的國家之一。
我已經在我的模型和冗長的控制器代碼中嘗試了各種範圍,但我無法弄清楚如何檢查應該是一個簡單的關係:Post.countries中是否至少有一個項目存在於User.countries中。
大大收到的任何幫助。
更新:
好了,我有我的控制器中的以下內容:
def index
@user = current_user
@user.countries.each do |user_country|
@user_country_posts += Country.find(user_country.id).posts
end
@posts = @user_country_posts
end
這是通過迭代user.countries並找到每個崗位對這些國家。但是當我運行它時,我得到:
NoMethodError: undefined method `+' for nil:NilClass
任何想法我做錯了什麼?
我不明白..只顯示帖子,至少有1個國家? – Lichtamberg