Tag: nested attributes

使用rails中的复合键更新模型嵌套属性

我有一个具有one_to_many关系的模型: class Work < ActiveRecord::Base has_many :work_right_holders accepts_nested_attributes_for :work_right_holders, allow_destroy: true end class WorkRightHolder < ActiveRecord::Base self.primary_keys = :work_id, :right_holder_id, :role belongs_to :work belongs_to :right_holder end 当我尝试使用嵌套属性更新work时,它会在关系中创建对象的新实例,而不是基于其主键更新现有实例: work.update( {“work_right_holders_attributes”=> { “0”=>{ “role”=>”Author”, “right_holder_id”=>”2”, “work_id”=>work.id, “share”=>”11” } } } ) 为什么会这样?

我无法使用accepts_nested_attributes_for创建模型对象。 它不会创建嵌套对象

我的模型结构如下所示: 董事会has_many主题。 主题has_manypost。 应用程序/模型/ board.rb class Board < ActiveRecord::Base has_many :topics end 应用程序/模型/ topic.rb class Topic < ActiveRecord::Base belongs_to :user belongs_to :board has_many :posts accepts_nested_attributes_for :posts validates :title, presence: true, length: { maximum: 255 } validates :user_id, presence: true validates :board_id, presence: true … end 应用程序/模型/ post.rb class Post < ActiveRecord::Base belongs_to :user belongs_to :topic […]

Rails:Polymorphic assosiation和accepts_nested_attributes

附件不会保存。 我在这里缺少什么? 在我的应用程序中,我有一个项目,每个项目用户都可以上传许多资产。 上传由载波完成。 这是模特 class Project :assetable,dependent: :destroy accepts_nested_attributes_for :assets, :allow_destroy => true end class Asset true mount_uploader :attachment, AttachmentUploader #carrierwave validates :attachment, presence: true validates :project_id, presence: true end 这些是我的project_controller中的动作 def new @project = Project.new @asset = @project.assets.build end def create @project = Project.new(project_params) @project.assets.build respond_to do |format| if @project.save format.html { redirect_to […]

导入csv文件时,如何在2个模型中进行质量分配?

我可以使用单个模型中的属性导入CSV文件并创建一个新对象(在本例中列出)。 我把它放在了Listing模型中 accepts_nested_attributes_for :address 其中address是关联模型(地址有很多列表,列表属于地址)。 我以为我能够在导入CSV文件时从地址模型中批量分配属性,但是我收到错误: Can’t mass-assign protected attributes: unit_number 其中unit_number在地址模型中的一个属性中(它在attr中可访问)。

获取fields_for和accepts_nested_attributes_for以使用belongs_to关系

我似乎无法使用Rails 2.3的新的accepts_nested_attributes_for工具在rails视图中为belongs_to关系生成嵌套表单。 我确实检查了许多可用的资源,看起来我的代码应该正常工作,但fields_for爆炸了我,我怀疑它与我如何配置嵌套模型有关。 我遇到的错误是一个常见的错误原因: ‘@account[owner]’ is not allowed as an instance variable name 以下是涉及的两个模型: class Account ‘User’, :foreign_key => ‘owner_id’ accepts_nested_attributes_for :owner has_many :users end class User < ActiveRecord::Base belongs_to :account end 也许这就是我在做’rong’的地方,因为帐户可以拥有’所有者’,可能是’用户’,但用户只有一个’帐户’,基于用户模型account_id键。 这是new.html.haml中的视图代码,它炸毁了我: – form_for :account, :url => account_path do |account| = account.text_field :name – account.fields_for :owner do |owner| = owner.text_field :name 这是新操作的控制器代码: class […]