私は4テーブルを持っていますusers
、user_details
、categories
およびgalleries
すべてのレコードを1つのクエリで表示したいと思います。私のテーブル構造の下を参照してくださいRuby on Railsで2つ以上のテーブルに参加するには?
カテゴリ
id
name
ユーザー
id
username
email
password
user_type
is_active (0 or 1)
created_at
modified_at
user_details
id
user_id
category_id
name
gender
address
phone
のギャラリー
id
user_id
name
image
is_active
私は、ユーザID = user_detailsとカテゴリと1とgalleries
テーブルからの彼女/彼女のすべてのis_active = 1の画像を表示するが、ここで
があるすべてのユーザー情報を取得したい私コード - ここ
@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()
私は取得していますエラーメッセージ:
Association named 'Category' was not found on User; perhaps you misspelled it?
実際には、私はUserDetails
モデルにCategory
を関連付けました。だから私はCategory
データを取得することができません。私のすべてのモデルを見てください -
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
はcategories
表が参加していない理由を私に教えてください。この質問に対して私を助けてください。
のようなもの:それはhas_manyのあるべきUserDetail:user_details – Thorin
いいえ、私のモデルアソシエーションは問題ありません。また、 'UserDetail'は私のためにうまく動作します。 – Chinmay235