How to delete a new_record object built with Rails

Parent model. Child models. Destroy (child model)


customer = Customer.new
customer.contatcs.build(name: '1')
customer.contatcs.build(name: '2')
customer.contatcs.build(name: '3')
customer.contatcs.build(name: '4')

If you want to erase the one with name 2

target = customer.contatcs.second
customer.contatcs.destroy(target)

Then you can erase it.

Recommended Posts

How to delete a new_record object built with Rails
How to manually generate a JWT with Rails Knock
[How to insert a video in haml with Rails]
How to get started with creating a Rails app
How to get along with Rails
Mapping to a class with a value object in How to MyBatis
[Rails] How to use rails console with docker
How to insert a video in Rails
How to rename a model with foreign key constraints in Rails
[rails] How to create a partial template
How to delete data with foreign key
How to build Rails 6 environment with Docker
[Rails] How to log in with a name by adding a devise name column
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
[Rails] rails new to create a database with PostgreSQL
[Rails] How to create a graph using lazy_high_charts
How to delete a controller etc. using a command
How to implement a like feature in Rails
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
[Rails] How to build an environment with Docker
How to make a follow function in Rails
Rails "How to delete NO FILE migration files"
How to write Rails
How to uninstall Rails
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
How to make batch processing with Rails + Heroku configuration
How to make a factory with a model with polymorphic association
[Rails] How to search by multiple values ​​with LIKE
[Rails] How to create a signed URL for CloudFront
How to push an app developed with Rails to Github
How to make an almost static page with rails
How to delete custom Adapter elements using a custom model
[Rails] How to deal with URL changes after render
How to delete untagged images in bulk with Docker
How to write a date comparison search in Rails
How to query Array in jsonb with Rails + postgres
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
[Docker + Rails] How to deal with Rails server startup failure
[Java] How to start a new line with StringBuilder
[Rails] How to install a decorator using gem draper
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to delete child elements associated with a parent element at the same time
How to deal with errors in Rails s could not find a JavaScript runtime.
[rails] How to post images
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to leave a comment
How to read rails routes
How to use rails join
How to number (number) with html.erb
How to update with activerecord-import
How to terminate rails server
How to write Rails validation