rails 4 scopes with NOT equal and associations -


i have instance variable in controller i'm trying convert scope model.

instance variable: products

@products_with_user_differences = product.where{|p| p.last_user != p.user.username && p.asset_type.name == "computer" unless p.user.nil? or p.allow_multi_users == true} 

explanation:

this shows products have last_user value different user.username type of "computer". excludes product user_id: nil or have allow_multi_users attribute set true.

i've tried following 0 luck: products.rb

scope :with_user_differences, -> { where(last_user != user.username && asset_type.name == "computer" unless user.nil? or allow_multi_users == true)} 

it doesn't seem recognize associations or allow "!=" in scope.

any ideas or pointers?

the following code return products associated user , asset_type

product.joins(:user, :asset_type) 

to exclude products allow_multi_users set true

product.where(allow_multi_users: false) 

to products asset_type.name computer, assuming followed convention , asset_type under table called asset_types

# take note of pluralized asset_type product.where(asset_types: { name: 'computer' }) 

to products last_user not equal associated user's username

product.where('products.last_user != users.username') 

given these, can following

def self.with_user_differences   product     .joins(:user, :asset_type)     .where(assset_types: { name: 'computer' })     .where(allow_multi_users: false)     .where('products.last_user != users.username') end 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -