Ruby on rails 连接表中的重复行具有\u many=>通过并接受\u嵌套的\u属性\u

Ruby on rails 连接表中的重复行具有\u many=>通过并接受\u嵌套的\u属性\u,ruby-on-rails,Ruby On Rails,一个活动有很多艺术家,一个艺术家有很多活动。艺术家和活动的连接模型是一种表演。我想将艺术家添加到活动中 除了在创建新事件时将重复的条目放入联接表之外,这是有效的。这在其他地方引起了问题 event.rb validates_presence_of :name, :location has_many :performances, :dependent => :destroy has_many :artists, :through => :performances accepts_n

一个活动有很多艺术家,一个艺术家有很多活动。艺术家和活动的连接模型是一种表演。我想将艺术家添加到活动中

除了在创建新事件时将重复的条目放入联接表之外,这是有效的。这在其他地方引起了问题

event.rb

validates_presence_of :name, :location

has_many :performances, :dependent => :destroy
has_many :artists, :through => :performances

accepts_nested_attributes_for :artists, :reject_if => proc {|a| a['name'].blank?}
# accepts_nested_attributes_for :performances, :reject_if => proc { |a| a['artist_id'].blank? }
艺术家.rb

has_many :performances
has_many :events, :through => :performances

has_attached_file :photo, :styles => { :small => "150x150>"  },
              :url  => "/images/artists/:id/:style/:basename.:extension",
              :path => ":rails_root/public/images/artists/:id/:style/:basename.:extension"

# validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']

validates_presence_of :name

has_many :mixes  
performance.rb

belongs_to :artist
belongs_to :event
事件\u控制器.rb

def new
  @event = Event.new
  @event.artists.build

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @event }
  end
end

def create
  @event = Event.new(params[:event])

  respond_to do |format|
    if @event.save
      flash[:notice] = 'Event was successfully created.'
      format.html { redirect_to(admin_events_url) }
      format.xml  { render :xml => @event, :status => :created, :location => @event }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
    end
  end
end
_form.html.erb

<% form_for([:admin,@event]) do |f| %>
<p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
</p>
<p>
    <%= f.label :location %><br/>
    <%= f.text_field :location %>
</p>
<p>
    <%= f.label :date %><br />
    <%= f.date_select :date %>
</p>
<p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
</p>
<% f.fields_for :artists do |builder| %>
    <p>   
        <%= builder.label :name, "Artist"%><br/>
        <%= builder.text_field :name %><br/>
    </p>
<% end %>
<p>
    <%= f.submit 'Submit' %> <%= link_to 'Cancel', admin_events_path %>
</p>
<% end %>

我刚刚用您提供的代码创建了一个快速测试rails应用程序,结果如下:

Processing EventsController#create (for 192.168.1.2 at 2010-06-16 00:33:05) [POST]
    Parameters: {"commit"=>"Create", "authenticity_token"=>"R8lKqeTIbRQ5Ft8K+TNMNusCh4qmnOv8xxSIi25MMNE=", "event"=>{"name"=>"Event", "artists_attributes"=>{"0"=>{"name"=>"Me"}}, "date(1i)"=>"2010", "location"=>"nowhere", "date(2i)"=>"6", "date(3i)"=>"16", "description"=>"Test Event"}}
      Event Create (0.4ms)   INSERT INTO "events" ("name", "location", "created_at", "updated_at", "date", "description") VALUES('Event', 'nowhere', '2010-06-16 04:33:05', '2010-06-16 04:33:05', '2010-06-16', 'Test Event')
      Artist Create (12.3ms)   INSERT INTO "artists" ("name", "created_at", "updated_at") VALUES('Me', '2010-06-16 04:33:05', '2010-06-16 04:33:05')
      Performance Create (0.2ms)   INSERT INTO "performances" ("name", "created_at", "event_id", "updated_at", "artist_id") VALUES(NULL, '2010-06-16 04:33:05', 1, '2010-06-16 04:33:05', 1)
    Redirected to http://192.168.1.5:3000/events/1
    Completed in 251ms (DB: 13) | 302 Found [http://192.168.1.5/events]
您正在运行什么版本的Rails

您是否尝试过创建一个干净的测试应用程序,看看它是否有效

可能有一个gem或某个插件导致了问题。我正在用SQLite3运行2.3.8

我没有得到您显示的所有额外输出,这可能是MySQL特定于has_many:through关系的内容,但这也是需要考虑的

以下是我的模式供参考:

  create_table "artists", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "events", :force => true do |t|
    t.string   "name"
    t.string   "location"
    t.date     "date"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "performances", :force => true do |t|
    t.string   "name"
    t.integer  "artist_id"
    t.integer  "event_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

通过将Rails更新到最新版本2.3.8解决了这个问题。这似乎是2.3.5中的一个bug


你的情妇长什么样?您可以在运行WEBrick的终端中看到它们。添加了event.rb和artist.rb的完整代码,我省略了这些代码,因为我相信这些代码与手头的问题无关。但我可能错了。验证、回形针和与其他模型的关系添加完整输出并注释掉第二个接受嵌套属性。结果似乎是一样的。@shalako:我认为应该可以解决它。捕捉得很好,但看起来我的代码就像你建议的那样是正确的,而我只是将它复制到stackoverflow中,结果是错误的这里更新了artists.rb,确认我的代码是正确的,再次运行测试。更新的输出。同样的行为。@shalako:我更新了我的答案。我现在要去睡觉了。我明天再来看看情况如何。祝你好运嘿听说你把它修好了,我很高兴。如果我对你的rails有帮助,你运行的是什么版本的rails?问题,请考虑我的问题或接受它作为答案。当你只回答问题时,似乎很难找到代表=
  create_table "artists", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "events", :force => true do |t|
    t.string   "name"
    t.string   "location"
    t.date     "date"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "performances", :force => true do |t|
    t.string   "name"
    t.integer  "artist_id"
    t.integer  "event_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end