假设我有这6个模型,例如
User
有一个Profile
然后,每个Profile
都包含gender_id
,country_id
和prefecture_id
。{
User
也有很多Cars
我正在说这样的联想。
模型/ user.rb
has_one :profile
has_many :cars
scope :confirmed, where("sign_in_count != ?",0)
paginates_per 10
模型/ profile.rb
belongs_to :user
belongs_to :gender
belongs_to :country
belongs_to :prefecture
模型/ gender.rb
has_many :user_profile
模型/ country.rb
has_many :user_profile
模型/ prefecture.rb
has_many :user_profile
模型/ car.rb
belongs_to :user
这是我的问题! 如何在控制器中修复代码并查看?
这是我目前的代码。
CONTROLLER
@users = User.confirmed.joins(:profile).page(params[:page]).order('profiles.total_point DESC')
查看
<% @users.each do |user| %>
<tr>
Username: <%= user.username %> <br />
Total Point: <%= user.profile.total_point %> <br />
Gender: <%= user.profile.gender.data %> <br />
Country: <%= user.profile.country.data %> <br />
Prefecture: <%= user.profile.prefecture.data %> <br />
The number of the cars that the user owns: <%= user.cars.count %> <br />
</tr>
<% end %>
答案 0 :(得分:1)
<强>更新强>
我想,您应该尝试这种方法:
@users = User.includes(:cars, profile: [:gender, :country, :prefecture]).confirmed.page(params[:page]).order('profiles.total_point DESC')
有效吗?