簡體   English   中英

如何從has_and_belongs_to_many模型關系訪問表

[英]How to access a table from a has_and_belongs_to_many model relationship

我有一個充當聯接表的模型,稱為CELEBRATIONS。

CELBERATION
has_and_belongs_to_many :users
belongs_to :board

 create_table :celebrations do |t|
      t.column :board_id,        :int, :null => false
      t.column :user_id,         :int, :null => false 
      t.column :role,            :string, :null => false 
      t.column :token,           :string
      t.timestamps
      end


USER
has_many :celebrations

Board
has_many :celebrations

慶祝表中的角色是:所有者,經理或朋友

我希望USERS記錄一個角色為FRIEND的BOARD。

我似乎丟失了一些東西。

 @invited_friends = User.find(:all, :include => :celebrations, :conditions => ["board_id = ?, role = ?", @board.id, "FRIEND"]) 

誰能指出我正確的方向? 先感謝您。

您的模型關系錯誤。 對於HABTM,

CELBERATION
has_and_belongs_to_many :users

USER
has_and_belongs_to_many :celebrations


And one more table celebrations_users with user_id, celebration_id columns.
Put the role column in users table.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM