2017-02-03 12 views
0

というcategory_managementgroupに属するユーザーが作成したlive_socialsという表示スコープを作成する際に問題が発生しています。スコープ以下複雑な複数の関連付けにスコープを書く -

Social.rb 
belongs_to :user 
scope :live_socials, -> {where(['date >= ?', Date.current])} 

CategoryManagementgroup.rb 
has_many :users 

User.rb 
has_many :socials 
belongs_to :category_managementgroup 

client_group呼ばcategory_managementgroup内のユーザーのすべての社交表示されます。

users.joins(:socials, :category_managementgroup).client_group.flat_map(&:socials) 

i am unsure how to extend the scope to display the live_socials (socials that have not expired). i tried the below but no success:

users.joins(:socials, :category_managementgroup).client_group.flat_map(&:socials).live_socials 

i get the below error:

2.3.0 :264 > ap users.joins(:socials, :category_managementgroup).client_group.flat_map(&:socials).live_socials 
    User Load (0.5ms) SELECT "users".* FROM "users" INNER JOIN "socials" ON "socials"."user_id" = "users"."id" INNER JOIN "category_managementgroups" ON "category_managementgroups"."id" = "users"."category_managementgroup_id" WHERE "category_managementgroups"."name" = 'Client Group' 
    Social Load (0.1ms) SELECT "socials".* FROM "socials" WHERE "socials"."user_id" = ? [["user_id", 10]] 
NoMethodError: undefined method `live_socials' for #<Array:0x007ff2f9834570> 
+1

はflat_map' ''前live_socials'を適用してみて、ユーザーにスコープの下に追加 'スコープ:live_socials、 - > {合流(:社交).where([ 'socials.date> =?'、 Date.current])} ' –

+0

すっごくありがとう!あなたのコメントを質問の答えにすることができますか?あなたの提案が完全に働いたので、私はそれにマークを付けます。 – ARTLoe

+0

回答として追加されました@ARTLoe –

答えて

1

flat_maplive_socialsスコープを適用してみて、以下の追加スコープUserモデル

scope :live_socials, -> { joins(:socials).where(['socials.date >= ?', Date.current])} 
関連する問題