Rails 2.3.x 中通过关联过滤记录有很多
我有三个模型通过 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
我想获取特定论坛的所有用户,其中用户的角色是朋友。该角色位于 Celebrations 表中。
我已在控制器中尝试了以下操作:
@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"])
I have three models connected via an has_many_through association.
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
I would like to get all of the users for a specific board where the role of the user is FRIEND. The role is in the Celebrations table.
I have tried the following in the controller:
@friends = User.condtions(:celebrations => {:role => "FRIEND", :board_id => session[:board_id]})
which results in:
NoMethodError in FriendsController#show
undefined method `condtions' for #<Class:0x1023e3688>
I have tried:
@friends = Board.find(session[:board_id]).celebrations.conditions(:role => "FRIEND").joins(:user)
which results in:
ArgumentError in FriendsController#show
wrong number of arguments (1 for 0)
How can I get the users who have the rols of FRIENDS for a specific board?
Thank you very much in advance.
This works:
board = Board.find(session[:board_id])
@friends = board.users.find(:all, :conditions => ["celebrations.role = ?", "FRIEND"])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 Board.rb 文件中扩展了类关联。
然后我可以在我的控制器中调用以下内容。
感谢 Josh Susser 和他的博客
I extended the class association in the Board.rb file.
And then I can call the following in my controller.
Thank to Josh Susser and his blog