- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有来自另一个非rails项目的数据库,所以我必须处理不寻常的列名。我有模型类别:
self.primary_key = "categoryID"
has_many :products, foreign_key: "category", primary_key: "categoryID"
以及型号产品:
self.primary_key = "productID"
belongs_to :category, foreign_key: "category", primary_key: "categoryID"
在Product
表中,有一个外键category
,它存储Category
表的主键,即 >类别ID
。我正在尝试在这样的控制台中创建一个产品:
c = Category.last
p = c.products.create
我收到一个错误:
ActiveRecord::AssociationTypeMismatch: Category(#29703600) expected, got Fixnum(#17843240)
我尝试了一些其他方法来创建一个产品,我可以在其中传递类别实例,但这会导致其他奇怪的错误。所以现在我只想以这种方式工作。哪里有问题?
最佳答案
我认为问题在于您有 category
列(因此 Rails 为其创建 category
方法)和具有相同名称的 category
关联.
您可以给协会起一个别的名字
我创建了测试应用
class CreateProducts < ActiveRecord::Migration
def change
create_table :products, id: false do |t|
t.integer :productID
t.integer :category
t.string :title
end
end
end
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories, id: false do |t|
t.integer :categoryID
t.string :title
end
end
end
class Product < ActiveRecord::Base
self.primary_key = :productID
belongs_to :my_category, class_name: 'Category', foreign_key: :category
end
class Category < ActiveRecord::Base
self.primary_key = :categoryID
has_many :products, foreign_key: :category
end
这样,下面的代码似乎可以正常工作
c = Category.create categoryID: 1, title: 'First category'
c.products # => []
c.products.create productID: 1, title: 'First product'
c.products.create productID: 2, title: 'Second product'
c.products # => #<ActiveRecord::Associations::CollectionProxy [#<Product productID: 1, category: 1, title: "First product">, #<Product productID: 2, category: 1, title: "Second product">]>
p = Product.first
p.category # => 1
p.my_category # => #<Category categoryID: 1, title: "First category">
关于mysql - Rails has_many/belongs_to 自定义数据库 ActiveRecord::AssociationTypeMismatch 得到 Fixnum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39679549/
我使用的是 Ruby on Rails 4.2。现在使用关系并尝试在 ContosoUniversity of .net MVC 示例中所示的 Instructor 和 Office_Assignme
我有以下关联 class Picture < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base h
我有 Category 和 belongs_to 类别的 Product。设置: mix phoenix.new shop cd shop mix ecto.create mix phoenix.ge
我的应用程序中有相当多的 belongs_to 关联,其中一些是可选的(即关联可以为 nil),一些是强制性的(关联必须是有效的父记录。 我最初的方法是使用我自己的验证方法来验证给定的 id(这里是强
我有以下事件记录类 class Car < ActiveRecord::Base belongs_to :owner end 在我尝试这个的代码中 Car.first.owner 它给我错误“未定
visits 有很多 visitsTasks 属于 tasks。 我正在查看 visits 表中的特定行 (visitID),并希望找到所有已完成的任务。在这些任务中,我想访问 path 和 mode
以前有人问过类似的问题,但我就是不明白。 所以我有 Model_A 和 Model_B。模型_B 属于 模型_A。我想做的是,当我创建 Model_A 时,自动调用 Model B 的 create
我有一个模型 class Survey :true end def update_saved_settings!(person=nil) self.responses_saved_d
我有 3 张属于同一批货的发票。创建的第一张发票始终是装运的主发票。 Invoice id shipment_id 21 55 # This is the main invoice 88
我有一个模型下载,带有一个表下载。 downloads 有一个名为 ip_address 的字段,它将 ip 地址存储为整数。我想设置一个 IpAddress 模型,但没有 ip_addresses
我有这个问题: 我有这段代码: 用户.rb: class User Name: Content: | 我在这里错过了什么?我无法访问 full_name 方法,但我不
所以我正在做一个应用程序,其中有一个“游戏”模型,该模型具有来自 4 个不同用户的 4 个属性。其中一个属性是 player_id,另外 3 个是用户 uid(facebook 给出的字符串 id)。
我有两个模型:Users 和 Posts。按照我的设置方式,帖子属于所有者(即用户)并且还有许多参与者(即用户)。在我的用户模型中,我想确保 owner 永远不属于 post。我在前端完成了此操作,但
用户.rb enum gender: [:Male, :Female] belongs_to :qualification 资格.rb has_many :users 当我查询时 User.all.g
用户有很多帖子。 class User has_many :posts end 帖子属于用户。 class Post belongs_to :user end 我正在尝试查找由管理员的用户发布
我正在使用两种不同的模型Person 和Organization。在许多其他属性中,Person 和 Organization 都可以是 Contractor。如果我只使用 Person 模型并想存储
拥有这些示例模型: class Post < ActiveRecord::Base belongs_to :category end class Category < ActiveRecord::
我有一个表格,学生可以在其中注册类(class)。当用户提交表单时,他就注册了类(class)并保存了他的付款信息。换句话说,一个 Enrollment对象被创建,Student对象已更新...除了我
我有 2 个表叫 Member和 MemberResume . MemberResume引用 Member在 key 上memberid . 在 MemberResume模型的关系是这样设置的: 'm
我有一个标记模型,具有与标记和可标记项目的多态关联。可标记项目都与 feed_item (has_one :feed_item) 有关联。我想将标记与 feed_item 相关联,即在 tagging
我是一名优秀的程序员,十分优秀!