标签: nested-attributes

在postgreSQL中实现1:N关系(对象关系)

我正在与 postgreSQL 作斗争,因为我不知道如何将一个类型 A 的实例链接到一组类型 B 的实例。我将给出一个简短的示例:

假设我们想要建立一个包含音乐专辑和人物的数据库,每个人都有一个他们最喜欢的专辑的列表。我们可以这样定义类型:

CREATE TYPE album_t AS (
Artist VARCHAR(50),
Title VARCHAR(50)
);

CREATE TYPE person_t AS (
FirstName VARCHAR(50),
LastName VARCHAR(50),
FavAlbums album_t ARRAY[5]
);
Run Code Online (Sandbox Code Playgroud)

现在我们要创建这些类型的表:

CREATE TABLE Person of person_t WITH OIDS;
CREATE TABLE Album of album_t WITH OIDS;
Run Code Online (Sandbox Code Playgroud)

现在,当我想让我的数据库成为对象关系型数据库时,我不想将专辑“对象”嵌套在表 Person 的 FavAlbums 行中,但我想“指向”表 Album 中的条目,这样n条Person记录就可以引用同一个Album记录,而无需一遍又一遍地重复。

我阅读了该手册,但似乎缺少一些重要的示例,因为对象关系功能并未经常使用。我也熟悉关系模型,但我想使用额外的表来表示关系。

postgresql object-relational-model nested-attributes

3
推荐指数
1
解决办法
5613
查看次数

手动创建参数哈希轨道

我需要使用rails使用的内部机制将字符串转换为适合创建具有嵌套属性的对象构建的参数哈希.我找不到请求rails的生命周期中的哪个实际上准备了params [] hash.

字符串看起来像:

"foo[butter]=fat&foo[cheese]=cheddar&bar[fork]=pointy&bar[knife]=sharp"
Run Code Online (Sandbox Code Playgroud)

只是为了给出一些上下文,上面的字符串是通过对我的app的API调用从加密的请求中提取的.这就是为什么它一旦解密就会以这种形式出现

def create
  decrypted = decrypt(params[:q])
  Object.create(magically_convert_to_param_hash(decrypted))
end
Run Code Online (Sandbox Code Playgroud)

我意识到我可以手动解析这个字符串并将其转换为所需的哈希值,但考虑到代码存在于已经完成它的框架中,它似乎有点潮湿.

ruby-on-rails nested-attributes

3
推荐指数
2
解决办法
1690
查看次数

传递参数以更新嵌套属性

我正在开发一个非常基本的练习应用程序,用户可以在其中创建多个引用.我遇到的问题是我无法更新我的报价.我有很多东西,并在谷歌和其他问题阅读,但无法弄清楚我做错了什么.这是我的代码:

#User Model
    class User < ActiveRecord::Base
      has_many :quotations, :dependent => :destroy
      attr_accessible :quotations
      accepts_nested_attributes_for :quotations, :allow_destroy => true
    end

#Quotations Model
class Quotation < ActiveRecord::Base
  attr_accessible :quote_text, :author, :quote_type, :category, :tags, :user_id
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

报价控制器

class QuotationsController < ApplicationController
  before_filter :get_user

  def get_user
    @user = User.find(params[:user_id])
  end 

  def edit
    @quotation = @user.quotations.find(params[:id])
  end

  def update
    @quotation = @user.quotations.find(params[:id]) 
    if @quotation.update_attributes(params[:id])
      redirect_to user_quotation_path :notice  => "Successfully updated quotation."
    else
      render :action => 'edit'
    end
  end    

end
Run Code Online (Sandbox Code Playgroud)

parameters ruby-on-rails parameter-passing nested-attributes

3
推荐指数
1
解决办法
1万
查看次数

嵌套属性无法正确显示表单

我有用户的表格

<%= form_for(@user) do |f| %>
  <%= f.fields_for :businesses do |field| %>
    <div class="field">
      <%= field.label :address %>
      <%= field.text_field :address %>
    </div>   
    <div class="field">
      <%= field.label :city %>
      <%= field.text_field :city %>
    </div>  
  <% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

它不显示我的字段,但是当我更改businesses为时business,它会显示,或者如果我f从 中删除f.fields_for. 但我认为它没有正确保存到数据库中。

我的用户模型

class User < ActiveRecord::Base
  has_many :businesses
  accepts_nested_attributes_for :businesses
en
Run Code Online (Sandbox Code Playgroud)

我的商业模式

class Business < ActiveRecord::Base
  attr_accessible :user_id, :address, :city
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

我的商业移民

class CreateBusinesses < ActiveRecord::Migration
  def change …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes ruby-on-rails-3 ruby-on-rails-3.2

3
推荐指数
1
解决办法
1865
查看次数

创建嵌套关联记录Ruby on Rails

显然我在下面的例子中忽略了一些非常简单的事情,我试图在实例化新的父记录时创建嵌套的关联记录.

我希望有一双新鲜的眼睛,我的帮助可以找到几天来一直在吃的东西.提前致谢!我错过了什么/弄乱了什么?这似乎是微不足道的.

ActiveRecord :: NestedAttributes显然不高兴.

class ContentGroup < ActiveRecord::Base
  attr_protected

  has_many :contents, :dependent=>:destroy

  accepts_nested_attributes_for :contents
end
Run Code Online (Sandbox Code Playgroud)
class Content < ActiveRecord::Base 
  attr_protected

  has_one :sort_item, :as=>:sortable
  belongs_to :content_group, :dependent=>:destroy

 accepts_nested_attributes_for :sort_item
end
Run Code Online (Sandbox Code Playgroud)
class SortItem < ActiveRecord::Base
  attr_accessible :position, :sortable_id, :sortable_type
  belongs_to :sortable, :polymorphic=>true
end
Run Code Online (Sandbox Code Playgroud)

在rails控制台中,轻度嵌套的调用按预期工作:

> p = {"sort_item_attributes"=>{"position"=>"1"}}
> b = Content.new(p)
 => Content id: nil, content_group_id: nil
Run Code Online (Sandbox Code Playgroud)

一个额外的巢爆炸:

> p = {"contents_attributes"=>{"sort_item_attributes"=>{"position"=>"1"}}}
> cg = ContentGroup.new(p)

 ActiveRecord::UnknownAttributeError: unknown attribute: position
    from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:88:in `block in assign_attributes'
    from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `each'
    from /.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails associations nested-attributes

3
推荐指数
1
解决办法
1586
查看次数

使用 simple_form 和 cocoon 时无法让 select2 工作

我有一个使用 simple_form、cocoon、bootstrap-sass 和 select2 的应用程序,它们都以相同的形式使用。我已经使用 select2 进行了所有操作,并为显示的第一个对象正确设置了样式,我相信这不是由 cocoon 设置的。但是,我使用 link_to_add_fields 选项添加的任何对象都没有正确的内嵌样式或使用 select2。

第一种形式如下所示:

这是表单中第一个对象的格式

这是 link_to_add_fields 添加对象的方式:

在此处输入图片说明

我试过使用回调在插入之前/之后调用 select2,但我无法让它工作。

希望有人能指出我正确的方向。

谢谢!

这是我的模型:

class Week < ActiveRecord::Base

  belongs_to :pool
  has_many   :games, dependent: :destroy

  accepts_nested_attributes_for :games

end
class Game < ActiveRecord::Base

  belongs_to :week

end
Run Code Online (Sandbox Code Playgroud)

控制器代码:

  def new
    @pool = Pool.find(params[:pool_id])
    if !@pool.nil?
      @week = @pool.weeks.new
      @game = @week.games.build
      @week.weekNumber = @pool.weeks.count + 1
    else
      flash[:error] = "Cannot create week. Pool with id:#{params[:pool_id]} does not exist!"
      redirect_to pools_path
    end
  end

  def create
    @pool = …
Run Code Online (Sandbox Code Playgroud)

javascript ruby-on-rails nested-attributes twitter-bootstrap jquery-select2

3
推荐指数
1
解决办法
2247
查看次数

不显示嵌套属性的必填字段的错误消息

就我而言,我的客户有很多任务(需要:detail和:completion_date字段。)。我一直在构建如下的嵌套模型形式:

= simple_form_for @client do |f|
  = f.simple_fields_for :tasks do |task|
    = task.input :detail
    = task.input :completion_date
  = f.submit
Run Code Online (Sandbox Code Playgroud)

提交表单时,如果没有空白的“详细信息”或“ completion_date”字段,则将重新呈现该表单,但不会显示任何错误消息。

我一直试图寻找解决方案。这些都没有提到对嵌套对象的属性进行验证失败。

希望任何人都能帮忙!谢谢,

ruby-on-rails nested-forms nested-attributes simple-form-for

3
推荐指数
1
解决办法
4355
查看次数

使用dict访问Python中嵌套的类实例

我试图在嵌套类中定义一个属性,然后使用字符串或字符串列表访问它.这是我正在尝试做的代码

class MyNestedClass(object):
    def __init__(self):
        self.att1 = 5.

class MyClass(object):
    def __init__(self):
        self.my_nested_inst = MyNestedClass()

my_inst = MyClass()
Run Code Online (Sandbox Code Playgroud)

我想改变my_inst.my_nested_inst.att1我所拥有的是一个像这样的列表的值:my_list = ['my_inst','my_nested_inst','att1'].

如果我用这个:

vars(vars(vars()[my_list[0]])[my_list[1]])[my_list[2]]
Run Code Online (Sandbox Code Playgroud)

这是有效的,但问题是我需要将它扩展到任意深度的嵌套实例.我无法找到一个好的方法来使用for循环工作.任何帮助是极大的赞赏.

此外,请注意,在全局命名空间中将字符串转换为变量名称已得到很好的解决,但这些答案似乎都不适用于此处.

编辑1:我会试着解释为什么我这样做,如果我做的很难解释,请告诉我.我使用scipy.optimize.fmin,我一直只使用4个参数进行优化.但是,我现在想扩展我的优化代码来处理任意数量的参数,其中一些是嵌套属性,在层/实例层次结构中有几层.我希望能够在顶层创建一个列表或字典,告诉fmin如何解压缩参数数组以设置嵌套属性.

python namespaces nested-attributes

2
推荐指数
1
解决办法
1186
查看次数

在closes_nested_attributes_for上打破模型,没有关联错误

我有一个Wine模特,一个葡萄酒可以有很多Varietals.

我有一个WineVarietals模型,并通过该表加入葡萄酒到品种.

当我放入accepts_nested_attributes_for :wine_varietals我的模型时,我得到 No association found for namewine_varietals'.它已经定义了吗?`

但我确实在我的模型中有关联.

我的葡萄酒模型是

class Wine < ActiveRecord::Base

    attr_accessible :name, :winery_id, :wine_varietals_attributes
    accepts_nested_attributes_for :wine_varietals

    belongs_to :winery
    has_many :wine_varietals
    #has_and_belongs_to_many :varietals, :join_table => :wine_varietals
    has_many :wine_photos
    has_many :vintages

我也有WineVarietal和Varietal模型.

如果我注释掉该accepts_nested_attributes_for行,则错误就会消失.

我尝试过不同形式的复数wines_varietals,wine_varietal但似乎无法摆脱这种错误.

ruby-on-rails nested-attributes

2
推荐指数
1
解决办法
2019
查看次数

ryanb / nested_form ## ActionView :: Helpers :: FormBuilder的未定义方法`link_to_remove'

我对gem nested_form有一点问题。我有:

class Factura < ActiveRecord::Base
 attr_accessible :itemfacturas_attributes
 has_many :itemfacturas
 has_many :productos, :through => :itemfacturas
 accepts_nested_attributes_for :itemfacturas, :reject_if => lambda { |a| a[:descripcion].blank? }, :allow_destroy => true
Run Code Online (Sandbox Code Playgroud)

和ItemFactura类

class Itemfactura < ActiveRecord::Base
 attr_accessor :vu, :vt, :descripcion
 belongs_to :factura
 belongs_to :producto
Run Code Online (Sandbox Code Playgroud)

我在facturas / new视图中使用了gem来添加itemfacturas。

<%= f.fields_for :itemfacturas do |b| %>
        <%= render 'itemfacturas/itemfacturas', f: b %>
 <% end -%>
 <%= f.link_to_add "Agregar item", :itemfacturas %>
Run Code Online (Sandbox Code Playgroud)

部分是:

<%= f.number_field :cantidad, :min => 0, :value => 1 %>
<%= f.text_field :descripcion, :class => …
Run Code Online (Sandbox Code Playgroud)

ruby gem ruby-on-rails nested-forms nested-attributes

2
推荐指数
1
解决办法
3789
查看次数