sql - Rails: Eager loading and advanced query on has_many :through -
Overview Whenever a new product is added to the database, So to determine whether it is a potential duplicate, it is examined against all existing products of the database. If a newly added product is found to be a potential duplicate, then a duplicate warning association is created for the newly added product (pending_product_id), and potential match (existing_product_id). In addition, an integer match score is stored on duplicate warning, which indicates the strength of probable matching between the two products. Model problem < P> How can I create the following question, and I can also include curious loading? Find products similar to some product variables where Product.duplicate_warnings.match_score & lt; 5 and make sure there is no duplicate warning with the match score in the same product & gt; 5? For example, something like this: Products where product.status == 0 and product.product == "Toyota" and Product.duplicate_warnings.match_score & lt ; 5 And make sure that the product matches match_core and gt; There is no duplicate warning with. 5? Do you want to load curious in your case? It will inquire about the products that follow your circumstances. but it will not be anxious load duplicate_warning so you will need more than one query. Alternatively, You can find all the products you want to exclude from before: and then load them with associations: Product. An ([ "status = 0 and = manufacturer (Toyota and ID?) Include", Exclude_product_ids]). (: Dupliket_warnins)
class product & lt; ActiveRecord :: Base has_many: matches, through :: duplicate_warnings, source :: current_product has_many: duplicate_warnings, foreign_key: "pending_product_id", Depend on dependent :: and class duplicate alarms & lt; ActiveRecord :: Connected to Base: Pending Product, Category_name: "Product" related_to: existing_product, class_name: "product" end
Product.joins (: duplicate_warnings). Selection ("Product. *, Maximum (duplicate_warnings.max_score) max_score"). Where (status: 0, manufacturer: "Toyota"). Group ("products.id"). ("Duplicate_warnings match_core)" 5 ")
exclude_product_ids = duplicateWarning.where ("match_score> 5") group ("pending_product_id"). (( Pending_product_id)
Comments
Post a Comment