2014-01-23 42 views
0

用的has_many 4验证错误我在与轨道4关联的麻烦创建记录它基本上进入和作者之间的关联,在被称为AuthorsEntry中间连接表。该架构如下:的Rails通过协会

class Entry < ActiveRecord::Base 
    validates :name, presence: true 
    validates :from, presence: true 
    validates :to, presence: true 

    belongs_to :event 
    has_many :authors, through: :authors_entry 
    has_many :authors_entry 

class AuthorsEntry < ActiveRecord::Base 
    validates :author, presence: true 
    validates :entry, presence: true 

    belongs_to :author 
    belongs_to :entry 
end 

class Author < ActiveRecord::Base 
    belongs_to :event 
    has_many :entries, through: :authors_entry 
    has_many :authors_entry 

    validates :name, presence: true 
    validates :event, presence: true 
end 

在我program_entries_controller.rb我有以下几种方法:

def create 
    @program_entry = Entry.new(program_entry_params) 

    author_ids_params.each do |id| 
     @program_entry.authors << AuthorsEntry.build(author_id: id) 
    end 

    @program_entry.event = @event 

    if @program_entry.save 
     flash[:notice] = t(:program_entry_created_successfully) 
     redirect_to organizer_event_program_entry_path(@event, @program_entry) 
    else 
     render :new 
    end 
    end 

def program_entry_params 
    params.require(:program_entry).permit(
     :name, :abstract, :'from(1i)', :'from(2i)', :'from(3i)', 
     :'from(4i)', :'from(5i)', :'to(1i)', :'to(2i)', :'to(3i)', :'to(4i)', 
     :'to(5i)' 
    ) 
end 

def author_ids_params 
    params.require(:program_entry).permit(:author_ids => []) 
end 

我已经提交保存在我的数据库中,创建行动应该只是增加一个新纪录为条目模型和关联(authors_entry)表。但是,当我尝试保存条目时,它总是返回authors_entry上的“is_invalid”。

+0

为什么你重复'验证:姓名,在场:真实'三次? –

+0

哎呀!错字,我会立即修复。 – Ingo86

回答

2

连接表应该命名为AuthorEntries遵循Rails约定。