Rails - has_many构建方法不保存关联

时间:2013-02-20 02:29:10

标签: ruby ruby-on-rails-3 model-associations

我在尝试进行协会工作时遇到了一些麻烦。

我的模特看起来像:

advertise.rb

class Advertise < ActiveRecord::Base

  belongs_to :user
  belongs_to :country

  has_many :targets
  # has_many :hss, :through => :targets

  validates :description, :presence => true
  validates :url, :presence => true
  validates :country_id, :presence => true
  validates :kind, :presence => true

  attr_accessible :description, :hits, :url, :active, :country_id, :kind

  KINDS = {
    '1' => 'Commoditie',
    '2' => 'Service',
  }

  HS = {
    '1' => 'Section',
    '2' => 'Chapter',
    '4' => 'Heading',
    '5' => 'SubHeading 1',
    '6' => 'SubHeading 2',
  }
end

hs.rb

class Hs < ActiveRecord::Base
  attr_accessible :code, :kind

  has_many :targets
  has_many :advertises, :through => :targets
end

target.rb

class Target < ActiveRecord::Base
  attr_accessible :advertise_id, :hs_id

  belongs_to :advertise
  belongs_to :hs
end

advertises_controller.rb

  def new
    @advertise = Advertise.new

    @countries = Country.all
  end

  def create
    @advertise = current_user.advertises.build(params[:advertise])

    if @advertise.save
      flash[:notice] = 'Advertise created with successful'
      redirect_to root_path
    else
      render :new
    end
  end

form用于制作新广告。

/advertises/new.html.haml

%table.table.table-striped
  %tr
    %td{:colspan => 2}= advertise.input :url, :required => true
  %tr
    %td{:colspan => 2}= advertise.input :description, :required => true, :as => :text, :input_html => { :cols => 50, :rows => 3 }
  %tr
    %td{:colspan => 2}= advertise.input :country_id, :collection => @countries, :as => :select, :label => 'Origin', :required => true
  %tr
    %td{:colspan => 2}= advertise.input :kind, :collection => Advertise::KINDS.map(&:reverse), :as => :select, :label => 'Type', :required => true
  %tr
    %td{:colspan => 2}= advertise.input_field :active, as: :boolean, inline_label: 'Active'

  =fields_for :targets do |target|
    %tr
      %td= select_tag :hs_kind, options_for_select(Advertise::HS.map(&:reverse).insert(0,'') )
      %td= target.select :hs_id, ''

hash参数:

[3] pry(#<AdvertisesController>)> params
=> {"utf8"=>"✓",
 "authenticity_token"=>"fOdn4NYLg/4HXruWURZPf9DYVT4EQzbaTRTKZvX1ugY=",
 "advertise"=>
  {"url"=>"http://test.com",
   "description"=>"test",
   "country_id"=>"17",
   "kind"=>"2",
   "active"=>"1"},
 "hs_kind"=>"2",
 "targets"=>{"hs_id"=>"487"},
 "commit"=>"Create Advertise",
 "action"=>"create",
 "controller"=>"advertises"}

哈希对我来说似乎没问题,但它只会创建Advertise,而不会为target关联创建advertise

是吗?也许是一个错误的模特协会。

提前致谢。

1 个答案:

答案 0 :(得分:1)

尝试更改

=fields_for :targets do |target|

= advertise.fields_for :targets do |target|

并将以下内容添加到advertise.rb

accepts_nested_attributes_for :targets
attr_accessible :targets_attributes  # just add targets_attributes to attr_accessible

请注意,执行此操作时,广告没有目标的对象将不会显示目标字段。你必须建立一个与广告相关联的新目标对象,以便显示字段

# example: on the new action of the controller
@advertise = Advertise.new
@advertise.targets.build