Tag: jointable

为什么我在Rails 4中获得连接表的未知主键例外?

这些是我的模特: class Product has_many :line_items has_many :orders, :through => :line_items end class LineItem belongs_to :order belongs_to :product end class Order has_many :line_items has_many :products, :through => :line_items end 来自schema.rb: create_table “line_items”, id: false, force: true do |t| t.integer “order_id” t.integer “product_id” t.integer “quantity” t.datetime “created_at” t.datetime “updated_at” end 我刚刚升级到Rails 4,我的连接表停止工作。 如果我执行@order.line_items ,它会抛出exception“模型LineItem中的表line_items的未知主键”。 @order.products按预期工作。 我已经尝试删除并重新创建line_items表,我已经尝试安装protected_attributes gem,但没有任何改变。 […]

无法通过关联更新has_many中的连接模型额外属性 – Rails

我仍然是Rails的学习者,正在制作一个Google Adwords模拟游戏,用于培训。 在游戏内部(我可能只解释这个问题的相关部分),我有三个模型,Adgroup(在我的代码中称为KeywordAdgroup), Keyword,AdgroupKeyword ,以及其中包含game_id的游戏模型。 这三种模式之间的关系是一个很好的结果has_many through: assciation,Adgroup通过AdgroupKeyword有很多关键字,而Keyword通过AdgroupKeyword有很多Adgroups。 现在,我想采用一种逻辑,即每个关键字只能添加到特定游戏的单个广告组中。 所以我在连接模型中添加了额外的属性game_id – AdgroupKeyword,并在此模型中采用validates_uniqueness_of :keyword_id, scope: :game_id 。 但是,我发现使用类似的代码,我可以使用.save方法validationCreate中的唯一性,但我无法使用.update(params)方法validationUpdate中的唯一性。 主要问题是,使用create和update的类似代码,create one可以save game_id save到连接模型(AdgroupKeyword),其中update(params)没有将game_id保存到连接模型,在控制台中,我可以看到update没有将game_id插入记录中。 它会变成像game_id: nil : # 但是save方法确实插入了game_id,当使用save方法插入时,game_id不是nil。 就像是 # 以下是我的代码: keyword_adgroup.rb (广告组的模型) class KeywordAdgroup < ApplicationRecord belongs_to :keyword_campaign has_many :adgroup_keywords, inverse_of: :keyword_adgroup, dependent: :destroy has_many :keywords, through: :adgroup_keywords, source: :keyword accepts_nested_attributes_for :adgroup_keywords, allow_destroy: true … accepts_nested_attributes_for […]

Rails:使用link_to外部链接调用控制器操作(并更新连接表)

在我的rails应用程序中有post,其中包含指向其他外部页面的链接( @post.link为我提供了链接)。 如果current_user单击链接,则应触发控制器操作,即viewed的操作,该操作应更新我的数据库,更具体地说是具有用户查看链接的信息的连接表。 到目前为止我已经做了什么: 在我的views / posts / index.html中: # Looping through all the posts 在我的posts_controller.rb中 def viewed @post = Post.find(params[:id]) @post.views.create(user_id: current_user.id) #respond_to do |format| # format.html { redirect_to posts_path } # format.js #end end 我创建了一个名为views的连接表,其user_id:integer和post_id:integer 。 在我的models / view.rb中 belongs_to :user belongs_to :post validates_uniqueness_of :post_id, scope: :user_id 在我的models / user.rb中 has_many :views, dependent: […]

的ActiveRecord :: HasManyThroughAssociationPolymorphicSourceError

我需要一个玩家拥有许多结构和属于玩家的结构。 结构是一种多态关系。 class Player player_structures end class PlayerStructures < ActiveRecord::Base belongs_to :structure, polymorphic: true belongs_to :player end class StructureA :structure has_one :player, :through => :player_structure end class StructureB :structure has_one :player, :through => :player_structure end 但是如果我拉出Player.first并询问它的结构,它会给出: ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association ‘Player#structures’ on the polymorphic object ‘Structure#structure’. 但它应该能够生成一个SQL查询,在其中找到所有带有id的player_structures,然后根据structure_id和structure_type获取结构。 为什么这会失败,我怎样才能有效地构建多态连接表? UPDATE 如果我按手动方式执行操作,则可以: player_structures.collect(&:structure) Rails,你不这样做吗?