- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理一个相当简单的 has_many through: 情况,我可以使 class_name/foreign_key 参数在一个方向上工作,但不能在另一个方向上工作。也许你能帮帮我。 (附注:我使用的是 Rails 4,如果这会产生差异):
英文:一个User通过ListingManager管理多个Listing,一个Listing由多个User通过ListingManager管理。列表管理器有一些数据字段,与这个问题无关,所以我在下面的代码中编辑了它们
这是有效的简单部分:
class User < ActiveRecord::Base
has_many :listing_managers
has_many :listings, through: :listing_managers
end
class Listing < ActiveRecord::Base
has_many :listing_managers
has_many :managers, through: :listing_managers, class_name: "User", foreign_key: "manager_id"
end
class ListingManager < ActiveRecord::Base
belongs_to :listing
belongs_to :manager, class_name:"User"
attr_accessible :listing_id, :manager_id
end
create_table "listing_managers", force: true do |t|
t.integer "listing_id"
t.integer "manager_id"
end
manager_id
而不是
user_id
user.listings
获取与用户关联的列表,我可以调用
listing.managers
获取与列表关联的经理。
user.listings
并没有多大意义。因为用户也可以“拥有”而不是“管理”列表,所以我真正想要的是
user.managed_listings
所以我调整了
user.rb
改变
listing.rb
中的代码完全类似。上面,所以我认为这应该可以正常工作。相反,我对这个 barfs 的 rspec 测试是这样说的
ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
Could not find the source association(s) :managed_listing or :managed_listings in model ListingManager. Try 'has_many :managed_listings, :through => :listing_managers, :source => <name>'. Is it one of :listing or :manager?
it "manages many managed_listings" do
user = FactoryGirl.build(:user)
l1 = FactoryGirl.build(:listing)
l2 = FactoryGirl.build(:listing)
user.managed_listings << l1
user.managed_listings << l2
expect( @user.managed_listings.size ).to eq 2
end
listing.rb
中使用的相同技术似乎在
user.rb
中不起作用.你能帮忙解释一下吗?
it "manages many managed_listings" do
l1 = FactoryGirl.create(:listing)
@user = User.last
ListingManager.destroy_all
@before_count = ListingManager.count
expect( @before_count ).to eq 0
lm = FactoryGirl.create(:listing_manager, manager_id: @user.id, listing_id: l1.id)
expect( @user.managed_listings.count ).to eq 1
end
PG::UndefinedColumn: ERROR: column listing_managers.user_id does not exist
(它应该在寻找“listing_managers.manager_id”)。所以我认为关联的用户端仍然存在错误。在
user.rb
的
has_many :managed_listings, through: :listing_managers, source: :listing
,用户怎么知道使用
manager_id
去它的列表(S)?
最佳答案
这里的问题是
has_many :managers, through: :listing_managers
ActiveRecord 可以推断出连接模型上的关联名称 (:listing_managers),因为它与
has_many :through
具有相同的名称。您正在定义的关联。也就是说,listings 和 listing_mangers 都有很多管理器。
has_many :listings
, 但用户
has_many :managed_listings
.因此 ActiveRecord 无法推断
ListingManager
上的关联名称它应该使用。
:source
选项适用于(见
http://guides.rubyonrails.org/association_basics.html#has-many-association-reference)。所以正确的声明是:
has_many :managed_listings, through: :listing_managers, source: :listing
(附注:您实际上并不需要其他
:foreign_key
上的
:class_name
或
has_many :through
选项。您可以使用这些选项来定义直接关联,然后在
has_many :through
上只需要指向正确的
:through
模型上的关联。)
关于ruby-on-rails - has_many :through with class_name and foreign_key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18154736/
我的模型是这样的: class Post :shareable, :primary_key => :guid, :foreign_key => :shareable_guid has_many
我有一个城市模型和城市表。服务模型和服务表。一项服务有一个城市(起源城市和目的地城市)。一个城市“有很多”服务。在 services 表中,有两列关于城市:origin_city_id 和destin
我有一个名为 animals 的表,其中包含以下列: id: INT state: VARCHAR 另一个名为 houses 的表具有以下列: id: INT animal_id: INT color
这是集群表: ╭────╥────────────┬─────────────╮ │ id ║ name │ prefix │ ╞════╬════════════╪══════
我得到了以下模型: class Coaching < ActiveRecord::Base ... belongs_to :user belongs_to :coach, class_na
我们有一些从提供非整数唯一 ID 的第三方导入的数据。 在 Rails 2.2.2 中,我们可以在 has_many 与非整数列的关系上使用 :foreign_key 并且它有效。 但是我们现在升级到
如果你有 Parent has_many :children Child Child 上的外键(到 Parent)和相应的 belongs_to :parent 是否有任何原因可能不受欢迎? 什么时
我正在使用 Rails 3,我有一个简单的模型 class Post has_many :comments end 我正在做一些元编程,我需要知道注释表中的 foreign_key 的名称。 在上
我正在处理一个相当简单的 has_many through: 情况,我可以使 class_name/foreign_key 参数在一个方向上工作,但不能在另一个方向上工作。也许你能帮帮我。 (附注:我
我有以下一组模型: class Cardstock :hex, :foreign_key => :hex has_many :palette_colors, :through => :color
在合适的 sqlite 版本中,我们可以通过“PRAGMA foreign_keys = ON”强制外键约束。但是,用户无法每次建立连接时都登录数据库。所以我想知道我们如何让它在 sqlalchemy
我最近遇到了这段代码。用户有很多答案。 :class_name 和 :foreign_key 的目的是什么? class Answer 'Question", :foreign_key => 'qu
我最近遇到了这段代码。用户有很多答案。 :class_name 和 :foreign_key 的目的是什么? class Answer 'Question", :foreign_key => 'qu
是否有一个 Postgresql 数据库配置等同于 Sqlite 中的 PRAGMA foreign_keys = ON? 最佳答案 您可以测试禁用表上的所有触发器,FK 是通过 PostgreSQL
假设您为电视节目创建了一个 imdb 类型的网站。您有一个 Show,其中包含许多附加的 episodes 和一群 people 现在我通过contribution 表将people 链接到episo
我有 2 个模型与 has_one 和 has_many 关联。 realm.rb class Realm < ActiveRecord::Base has_one :realm_type,
我在一个实体中有两个外键引用另一个实体。这是它的样子 class Review(db.Model): __tablename__ = 'Review' id = db.
从文档中我读到: class Book < ApplicationRecord belongs_to :author, class_name: "Patron", foreign_key: "pa
我用 Flask-SQLalchemy 创建了 3 个模型:用户、角色、用户角色 角色.py: class Role( ActiveRecord, db.Model ): __tablenam
我创建了一个 SQL 文件,它加载到 sqlite3 中并为我创建了一堆表。在该 sql 文件中,我尝试使用 pragma 强制执行 foreign_keys: PRAGMA foreign_keys
我是一名优秀的程序员,十分优秀!