d3bug3r
d3bug3r

Reputation: 2586

Ruby on Rails table relationship

I have a product table where it list down all the products user created. In product table there is country which indicate the product origin. Now to make the product country visible, the user table must have merchant status of 1.

Following code is use to pull the countries from product table:

@countries = Product.select(:country).distinct.where("country is not null and country <> ''").pluck(:country)

Product Model:

class Product < ActiveRecord::Base
belongs_to :user
end

User Model:

class User < ActiveRecord::Base
has_many :products
end

How can I modified the above code, so it will include merchant_status in user table? Means I need list down countries only if merchant_status = 1 in User table. Thanks!!

Upvotes: 0

Views: 119

Answers (1)

Vishal Jain
Vishal Jain

Reputation: 1940

you can try this way:

Product.joins(:user).where("country is not null and country <> ''").where(:users => {:merchant_status => 1}).pluck(:country)

Upvotes: 1

Related Questions