一个挺有意思的问题,想了半天没有解决办法。
情景是这样的:系统中有两种category,system_category和user_category,所有属性都一样,只是一个是系统分类,一个是用户自己的分类。比如javaeye就是有这样的机制。用户发文时可以同时选择system_category和user_category。假如数据库的设计是system_category和user_category都在一张表categories中。那么Category和Article类的代码大致如下:
# Category.rb
class Category < ActiveRecord::Base
has_many :articles
end
# Article.rb
class Article < ActiveRecord::Base
belongs_to :system_category, :class_name => "Category", :foreign_key => "system_category_id"
belongs_to :user_category, :class_name => "Category", :foreign_key => "user_category_id"
end
各位肯定已经注意到了,两边的has_many和belongs_to不是一一对应的。这个时候执行一些语句,结果如下(假设id为1的user cateogry有两个article,id分别为1,2):
Article.find_by_id(1).user_category.id # => 1
Article.find_by_id(2).user_category.id # => 1
Category.find_by_id(1).articles # 出错,说articles没有category_id。
出现以上错误是正常的,但是有什么方法可以解决这个问题吗?当然,我可以在category中声明两个has_many,但是这样显然不符合应用逻辑。我也尝试过使用:finder_sql,但是不知道怎么得到当前category的id,也就没办法手工做查询了。
分享到:
- 2007-07-19 18:15
- 浏览 3979
- 评论(8)
- 论坛回复 / 浏览 (8 / 5642)
- 查看更多
相关推荐
避免改动缺省的 ActiveRecord(表的名字、主键,等等),除非你有一个非常好的理由(像是不受你控制的数据库)。 把宏风格的方法放在类别定义的前面(has_many, validates, 等等)。 偏好 has_many :through 胜...
支持many_to_one / belongs_to,one_to_many / has_many,many_to_many / has_and_belongs_to_many和has_one / one_to_one关联 以不违反外键约束的方式加载灯具的依赖关系图 有一个非常简单的API...
该插件使用PostgreSQL数组的功能实现了在Rails中进行has_and_belongs_to_many关联的替代方法。 在很多情况下,你只需要或的功能使用许多一对多与连接表是不必要的传统方法。 我们可以只存储id的整数数组。 它是如何...
has_many和belongs_to其实是Topic类的class method,标准写法是: 代码如下: class Topic < ActiveRecord::Base Topic.has_many(:posts) Topic.belongs_to(:user) end 那么has_many可以给我们带来什么呢?...
提供一种将一个模型的定义为关联模型的方法。 例子 首先,两个简单的Article和Comment模型: class Article < ActiveRecord :: Base has_many :comments end class Comment < ActiveRecord :: Base belongs...
has_and_belongs_to_many 一个简单的DSL,用于配置要复制的字段。 DSL可以应用于您的Rails模型或即时使用。 支持STI(单表继承)子代继承其父变形虫设置。 多种配置样式,例如包含,排除和不
强大的服务器端搜索,排序和筛选ActiveRecord类(具有belongs_to和has_many关系)。 通过搜索sql列以及ActiveRecord和Array集合中的计算值来做正确的事情。 显示基于current_user授权操作的关联编辑/显示/销毁...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。博客类别在本课程中,我们将设置一个...
查询方法扩展 在 rails 4 扩展查询 activerecord 安装 将此行添加到应用程序的 Gemfile 中: ... belongs_to :store has_many :books end // Category ( id : integer , name : string , store_id : integer ) cla
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。博客类别在本课程中,我们将设置一个...
to,has_many和has_and_belongs_to_many 验证对象数据,包括嵌入式对象类表扩展的行为-例如Model_Ferrari扩展Model_Car扩展芒果。 其他ORM用户非常熟悉的非常简单的语法芒果数据库一个访问MongoDB的简单包装器。 将...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。 博客类别 在本课程中,我们将设置...
ActsAsInheritable 充当可继承对象是专门为Rails / ActiveRecord... ActiveRecord :: Base belongs_to :parent , class : Person has_many :children , class : Person , foreign_key : :parent_id has_many :grandc
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。博客类别在本课程中,我们将设置一个...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。博客类别在本课程中,我们将设置一个...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。博客类别在本课程中,我们将设置一个...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。 博客类别 在本课程中,我们将设置...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。 博客类别 在本课程中,我们将设置...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。 博客类别 在本课程中,我们将设置...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。 博客类别 在本课程中,我们将设置...