has_and_belongs_to_many and nested attributes

26 views
Skip to first unread message

Roque Pinel

unread,
May 7, 2015, 2:37:58 AM5/7/15
to rubyonra...@googlegroups.com
I'm not sure if I missed something.

In the following case, when the join table is not a model, should it be possible to build the relation with nested attributes?

unless File.exist?('Gemfile')
 
File.write('Gemfile', <<-GEMFILE)
    source
'https://rubygems.org'
    gem
'rails', github: 'rails/rails'
    gem
'arel', github: 'rails/arel'
    gem
'sqlite3'
  GEMFILE


  system
'bundle'
end


require 'bundler'
Bundler.setup(:default)


require 'active_record'
require 'minitest/autorun'
require 'logger'


# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)


ActiveRecord::Schema.define do
  create_table
:posts, force: true  do |t|
 
end


  create_table
:tags, force: true  do |t|
 
end


  create_table
:post_tags, id: false, force: true  do |t|
    t
.integer :post_id
    t
.integer :tag_id
 
end
end


class Post < ActiveRecord::Base
  has_and_belongs_to_many
:tags
  accepts_nested_attributes_for
:tags
end


class Tag < ActiveRecord::Base
end


class BugTest < Minitest::Test
 
def test_habtm_nested_attributes
    post
= Post.create!
    tag
= Tag.create!
    post
.tags_attributes = [{ tag_id: tag.id }]
   
assert post.save
 
end
end

Thanks

Reply all
Reply to author
Forward
0 new messages