Tag: has many through

如何在Rails 3中为社交网络应用程序实现友谊模型?

我目前正在开发一个小型社交网络应用程序,现在我正在尝试创建一个代表用户之间友谊的模型。 这是我到目前为止提出的: class User :friendships end class Friendship ‘User’ end 我的友谊模型有一个确认为布尔值的字段,我想用它来定义一个待定或确认的友谊。 如何访问特定用户的所有待处理请求? 我可以使用Rails的范围方法以某种方式定义它吗? 就像是 current_user.friendships.requests # => [Friendship, Friendship, …] 会很好。 如何使这种关联双向化? 一旦确认了朋友请求,我就会添加另一个友谊,这样我的友谊表看起来就像这样: | user_id | friend_id | confirmed | ———————————– | 1 | 2 | true | | 2 | 1 | true |

Rails – 按连接表数据排序

我在工作中有一个RoR项目。 以下是我的模型的适用部分。 家 has_many :communities, :through => :availabilities has_many :availabilities, :order => “price ASC” 社区 has_many :homes, :through => :availabilities has_many :availabilities 可用性 belongs_to :home belongs_to :community 数据库中的“可用性”表具有附加数据列“价格” 所以现在我可以打电话了 @home.availabilities.each do |a| a.community.name a.price 并按我的要求取回按价格订购的可用性数据。 我的问题是: 有没有办法通过avaliabilities.first.price (第一个=最低)自动订购Homes? 也许是default_scope :order东西default_scope :order ?

使用has_many:通过和构建

我有三个模型,都是为了一个has_many:通过关系。 它们看起来像这样: class Company < ActiveRecord::Base has_many :company_users, dependent: :destroy has_many :users, through: :company_users accepts_nested_attributes_for :company_users, :users end class CompanyUser < ActiveRecord::Base self.table_name = :companies_users #this is because this was originally a habtm relationship belongs_to :company belongs_to :user end class User < ActiveRecord::Base # this is a devise model, if that matters has_many :company_users, dependent: […]