How to Join more than 2 tables in Ruby?

ساخت وبلاگ

Vote count: 0

I have 4 tables users, user_details, categories and galleries I want to show all record in one query. See below my table structure

categories

id
name

users

id
useame
email
password
user_type
is_active (0 or 1)
created_at
modified_at

user_details

id
user_id
category_id
name
gender
address
phone

galleries

id
user_id
name
image
is_active

I want to get all the user information which user id=1 with user_details and category and show her/her all the is_active=1 images from the galleries table

Here is my code -

@user_details = User.eager_load(:Category,:Gallery,:UserDetail).where('users.id'=>20, 'users.is_active'=>1, 'users.user_type'=>2, 'galleries.is_active'=>1).first()

Here I am getting error message:

Association named 'Category' was not found on User; perhaps you misspelled it?

Actually I have associate Category in the UserDetails model. So I am unable to get the Category data. Please see my all models -

category.rb

class Category < ActiveRecord::Base has_many :UserDetail
end

user.rb

class User < ActiveRecord::Base #before_save :encrypt_password has_one :UserDetail, dependent: :destroy has_many :Gallery, dependent: :destroy
end

user_detail.rb

class UserDetail < ActiveRecord::Base belongs_to :User belongs_to :Category
end

gallery.rb

class Gallery < ActiveRecord::Base belongs_to :User
end

Let me know why categories table not joining. Please Help me for this query.

asked 1 min ago

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : نویسنده : استخدام کار backsoft بازدید : 329 تاريخ : جمعه 31 ارديبهشت 1395 ساعت: 18:41