- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在这里看过关于 self 参照关系的 railscast:http://railscasts.com/episodes/163-self-referential-association
我在此基础上添加了一个关于友谊的“状态”字段,以便必须请求和接受友谊。 'status' 是一个 bool 值——false 表示尚未回复,true 表示已接受。
我的问题是在给定 current_user(我正在使用 Devise)和另一个用户的情况下想出一种查找友谊对象的方法。
这是我可以使用的:
current_user.friends # lists people you have friended
current_user.inverse_friends # lists people who have friended you
current_user.friendships # lists friendships you've created
current_user.inverse_friendships # lists friendships someone else has created with you
friendship.friend # returns friend in a friendship
我希望获得类似于以下的方法,以便我可以轻松地检查友谊的状态:
current_user.friendships.with(user2).status
这是我的代码:用户.rb
has_many :friendships
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user
友谊.rb
belongs_to :user
belongs_to :friend, :class_name => "User"
当我这样做时——为了显示用户的 friend ,我必须同时显示“current_user.friends” 和“current_user.inverse_friends”——有没有有什么方法可以调用 "current_user.friends" 并将其加入两者?
最佳答案
您可以将条件传递给给定的关联:
has_many :friends, :class_name => 'User', :conditions => 'accepted IS TRUE AND (user = #{self.send(:id)} || friend = #{self.send(:id)})"'
注意:我们使用 send,因此它不会评估属性,直到它尝试将它们取出。
如果你真的想要 ".with(user2)"语法,那么你可以通过 named_scope 来实现,例如
Class Friendship
named_scope :with, lambda { |user_id|
{ :conditions => { :accepted => true, :friend_id => user_id } }
}
end
应该允许:
user1.friendships.with(user2.id)
注意:代码未经测试 - 您可能需要修复错误...
关于ruby-on-rails - ruby rails : Retrieving a friendship given two users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6735920/
我有一条路线,有两个与之关联的模型,如下所示: App.IndexRoute = Ember.Route.extend({ model: function() { return Emb
这是我的第一个 Python 脚本,所以我认为我做错了什么。但是我在任何教程或示例中都找不到线索。以下代码(可以这么说): import urllib urllib.retrieve("http://
SDK:适用于 Go 0.5.0 的 Apache Beam SDK 我们在 Google Cloud Data Flow 中运行 Apache Beam Go SDK 作业。他们一直工作得很好,直到
我希望简化网站用户在 Adobe Business Catalyst 网站上注册、登录和检索/重置密码的流程。 我已成功使用 AJAX 提交安全区登录表单。我还使用 jQuery.load 从密码
我在 R 中收到以下错误消息: Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set", : Unable to r
在什么情况下“反文档频率”在信息检索中不起作用? 最佳答案 如果您不希望在系统中权衡稀有术语而不是频繁出现的术语,则可能不希望使用IDF。此外,计算idf是一项昂贵的操作。从以下事实可以明显看出这一点
我正在尝试计算 Average Precision (和 Mean Average Precision )在 Oxford Building image dataset 上. 下面是他们提供的用于计算
有人知道在网站上查找最相关的联系信息的脚本/食谱/库吗? 一些可能的情况: 在个人网页上查找联系电话号码 在博客上查找所有者电子邮件地址 查找联系页面的网址 最佳答案 查看WSO2's Mashup
我看过像“sc2ranks.com”这样的网站,我完全想知道他们是如何获得我只能在游戏中访问的信息的。我的意思是我如何访问星际争霸 II 欧盟天梯?这只是我们必须访问/下载的 URL,还是需要通过注入
我已阅读 Permuterm indexes stanford 网站上的页面,但是我仍然无法弄清楚我们如何从以下地址到达:*X*至 X* . 那么$在哪里? ? 我可以得到这些: For X, loo
考虑以下选定的 html 片段: .......... .......||||||||||||||||||||||||||||||....... 第二行代表用户选择(管道),跨越多个跨度标签。使用 j
我需要从我的 GitHub 私有(private)仓库下载一个文件。因此,按照 GitHub 站点上的说明,我为我的凭据创建了一个 OAuth token 。 然后我执行了这个 PS 脚本: $Web
我有几个扩展 C 的类,并且我需要一个接受 C 类型的任何参数的方法。但是在这个方法中我想知道我正在处理 A 还是 B。 * public A extends C public B extends C
我正在尝试学习 Apache Struts 框架,并且我编写了一个用于进行类(class)注册的小型应用程序,但每当我尝试加载我的应用程序时,它都会抛出以下异常: javax.servlet.Serv
有点跟进 my self-answered question about finding the column names. 在 UniVerse 中,您无法查询文件的所有列,除非文件字典中的 @ 短
我正在为 Dynamics CRM 2011 创建一个插件,以便在用户打开潜在客户时执行。所以我使用 Retrieve 消息来执行插件。 IPluginExecutionContext context
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
正在尝试检索文章的文本。我想选择 中的所有文本 ... 我做到了。 但我只想从文章正文中检索文本,而不是整个页面 Document article = Jsoup.connect("html doc
我一直在尝试元素存储,但我被元素存储中的 .retrieve(); 问题所困扰。 文档指出: Element:retrieve actually accepts an optional second
尝试对 facebook 进行图形 API 调用时出现以下错误。我知道我的回调 url 很好,因为当我在 facebook 调试器中测试它时,它可以很好地查看页面。我也在使用 Google App E
我是一名优秀的程序员,十分优秀!