Ruby ActiveRecord:具有多个到(两次)
在我要找的Ruby ActiveRecord:具有多个到(两次),ruby,activerecord,Ruby,Activerecord,在我要找的地方有东西。一件东西可以在许多不同的地方s,许多东西可以在一个地方 class Thing < ActiveRecord::Base has_and_belongs_to_many :places end class Place < ActiveRecord::Base has_and_belongs_to_many :things end 这看起来对吗?效率高吗?谢谢。我认为你的思路是对的,我要做的最大改变是,与其拥有一个连接表(places\u things
地方有东西。一件东西可以在许多不同的地方
s,许多东西可以在一个地方
class Thing < ActiveRecord::Base
has_and_belongs_to_many :places
end
class Place < ActiveRecord::Base
has_and_belongs_to_many :things
end
这看起来对吗?效率高吗?谢谢。我认为你的思路是对的,我要做的最大改变是,与其拥有一个连接表(places\u things),不如让它成为一个合适的模型。我决定称之为存在
数据只存在于一个地方,因此它被正确地规范化了。这些关系很清楚,很容易管理。我认为这很有效
class Place < ActiveRecord::Base
has_many :existences
has_many :things, :through => :existences
end
class Thing < ActiveRecord::Base
has_many :existences
has_many :places, :through => :existences
end
class Existence < ActiveRecord::Base
belongs_to :place
belongs_to :thing
end
class Find < ActiveRecord::Base
belongs_to :user
belongs_to :existence
end
class User < ActiveRecord::Base
has_many :finds
has_many :existences, :through => :finds
has_many :things, :through => :existences
end
classplace:存在
结束
类对象:存在
结束
类存在:找到
有很多东西,:通过=>:存在
结束
您将需要Rails3.1来完成嵌套的has,就像我们在User中所做的那样
顺便说一句,当你说你需要Rails 3.1时,正确的关联声明应该是:归属于:放置东西,你是说ActiveRecord 3.1吗?我使用的是ActiveRecord 3.0.9,我认为它是Rails之外最新的gem(事实上是Sinatra),当我尝试user.things
。我不认为你会想使用Rails 3.0.x和AR 3.1。我相信你会发现一些有趣的虫子。Rails 3.1现在是RC6版本,所以如果你想使用Rails 3.1,你需要在gem文件中包含gem“Rails”“~>3.1.rc”。实际上我根本没有使用Rails,我使用的是AR和Sinatra。你知道我需要什么来获得嵌套的:has_many:throughs吗?试试:gem安装activerecord--然后再要求'activerecord'。
class Place < ActiveRecord::Base
has_many :existences
has_many :things, :through => :existences
end
class Thing < ActiveRecord::Base
has_many :existences
has_many :places, :through => :existences
end
class Existence < ActiveRecord::Base
belongs_to :place
belongs_to :thing
end
class Find < ActiveRecord::Base
belongs_to :user
belongs_to :existence
end
class User < ActiveRecord::Base
has_many :finds
has_many :existences, :through => :finds
has_many :things, :through => :existences
end