私は次のモデルを持っています:
class Post < ActiveRecord::Base
has_and_belongs_to_many :countries
end
class User < ActiveRecord::Base
has_many :entitlements
has_many :countries, :through => :entitlements
end
投稿インデックス ページの投稿には、ユーザーの国と同じ国が少なくとも 1 つ含まれている必要があります。
モデルと長いコントローラー コードでさまざまなスコープを試しましたが、単純な関係を確認する方法がわかりません。つまり、Post.countries の少なくとも 1 つの項目が 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
私が間違っていることはありますか?