2016-03-29 14 views
0

私はproductテーブルを持っています。そこにはユーザが作成したすべての製品がリストされています。製品表には、製品の起源を示すcountryがあります。今の製品の国が表示されるように、ユーザテーブルには、コードに続いて1Ruby on Railsテーブルの関係

merchant statusを持っている必要があり、製品テーブルから国を引っ張るために使用することである:

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

商品型番:

class Product < ActiveRecord::Base 
belongs_to :user 
end 

ユーザーモデル:

class User < ActiveRecord::Base 
has_many :products 
end 

上記のコードを変更するにはどうすればよいですかユーザーテーブルにmerchant_statusが含まれますか?ユーザーテーブルにmerchant_status = 1がある場合にのみ、私は国をリストダウンする必要があります。ありがとう!!

+2

あなたはこの 'Product.joins試すことができます(ユーザ).where( "国がnullと国ではありません。<> ''").where(:ユーザーを= > {:merchant_status => 1})pluck(:国) ' –

+0

@ VishalJAINありがとうございます!それはうまく動作します – d3bug3r

+0

@ VishalJAIN答えとして投稿し、OPがそれを受け入れるようにしてください。 –

答えて

1

あなたはこの方法を試すことができます。

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