У меня есть три модели, связанные через ассоциацию has_many_through.
class Board < ActiveRecord::Base
has_many :celebrations, :dependent => :destroy
has_many :users, :through => :celebrations
class User < ActiveRecord::Base
has_many :boards,
:through => :celebrations
has_many :celebrations, :dependent => :destroy
class Celebration < ActiveRecord::Base
belongs_to :user
belongs_to :board
class CreateCelebrations < ActiveRecord::Migration
def self.up
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
end
Я хотел бы получить всех пользователей для конкретной доски, роль которой в ней - FRIEND.Роль в таблице торжеств.
Я попробовал следующее в контроллере:
@friends = User.condtions(:celebrations => {:role => "FRIEND", :board_id => session[:board_id]})
, что приводит к:
NoMethodError in FriendsController#show
undefined method `condtions' for #<Class:0x1023e3688>
Я пытался:
@friends = Board.find(session[:board_id]).celebrations.conditions(:role => "FRIEND").joins(:user)
, что приводит к:
ArgumentError in FriendsController#show
wrong number of arguments (1 for 0)
Как я могу получить пользователей, у которых есть права на ДРУЗЬЯ для конкретной доски?
Большое спасибо заранее.
Это работает:
board = Board.find(session[:board_id])
@friends = board.users.find(:all, :conditions => ["celebrations.role = ?", "FRIEND"])