Rails query not equal -


my rails app has 3 components: -users (has many works , comments) -works (blog posts) (belongs users, has many comments) -comments (comments belong both users , work)

i'm trying display comments on page particular user except comments made on blog posts belong him/her. should query in controller?

here have far. not load comments @ all.

    @user = user.find(params[:id])     @works = work.pluck(:user_id)     @comments = @user.comments.where(is_boolean_value: true).where(['user_id not in(?)', @works]).all 

how can change query shows comments made user user did not own work?

thanks! -b

the first problem

work.pluck(:user_id) 

that fetches user_id user has object of table work. instead, fetch work ids items user created:

@works = @user.works.map {|obj| obj.id} 

then ask database give comments belong user not belong 1 of works:

@comments = @user.comments.where('work_id not in(?)', @works) 

Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -