in Education by
I have this example code which raises a "Duplicate Expection". Instead, I want just to skip if a duplicate exists. (1..10).each do |page| group.products << [{id: 1, title: "Example A"}, {id: 2, title: "Example B"}] group.save end The product array is static just for this example, what matter is that it has ID's, so products exist in the database. The problem is when I assign a product that I have already assigned in the past which raise a "Not Unique exception". In this scenario, I would just skip the association. How is that possible? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You could use create_with in combination with find_or_create_by: (1..10).each do |page| group.products << Product.create_with(title: "Example A").find_or_create_by(id: 1) group.products << Product.create_with(title: "Example B").find_or_create_by(id: 2) group.save end

Related questions

0 votes
    I have following Comments-on-Models design (source: http://railscasts.com/episodes/154-polymorphic-association): ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    having this code block of an example rails model class: class Block < ActiveRecord::Base has_many :bricks, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
0 votes
    How do you update the records with or without using Update Strategy?...
asked Mar 28, 2021 in Technology by JackTerrance
0 votes
    __________ scheme that records only information about dirty pages and associated information and does not even require ... topic in section Recovery System of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    If A and B are two events associated with a random experiment for which P(A) = 0.60, P(A or B) = 0.85 and P(A and B) = 0.42, find P(B). Select the correct answer from above options...
asked Nov 22, 2021 in Education by JackTerrance
0 votes
    Let A and B be two events associated with a random experiment for which P(A) = 0.4, P(B) = 0.5 and P(A or B) = 0.6. Find P(A and B). Select the correct answer from above options...
asked Nov 22, 2021 in Education by JackTerrance
0 votes
    I'm wanting a method called same_url? that will return true if the passed in URLs are equal. The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I just realized that the l (or localize) method in Rails (3.1.3 at the moment) appears to not ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Make Twenty (20) SQL questions including create table, insert, update, delete, join, string operation, aggregate ... books or webs Select the correct answer from above options...
asked Dec 9, 2021 in Education by JackTerrance
0 votes
    Make Twenty (20) SQL questions including create table, insert, update, delete, join, string operation, aggregate ... books or webs Select the correct answer from above options...
asked Dec 3, 2021 in Education by JackTerrance
0 votes
    If A and B are two events associated with a random experiment such that P(A) = 0.25, P(B) = 0.4 and P(A or B) ... ) (ii) P(A and bar B) Select the correct answer from above options...
asked Nov 22, 2021 in Education by JackTerrance
...