Skip to content

Instantly share code, notes, and snippets.

@wethu
Created February 17, 2014 07:20
Show Gist options
  • Save wethu/9046159 to your computer and use it in GitHub Desktop.
Save wethu/9046159 to your computer and use it in GitHub Desktop.
Trouble again with Checkboxes.. :(
class ChosenProperty < ActiveRecord::Base
has_and_belongs_to_many :job_values
end
~/www/comprint $ rails c
Loading development environment (Rails 4.0.2)
[1] pry(main)> j = Job.new
=> #<Job id: nil, client_id: nil, product_id: nil, quantity: nil, description: nil, created_at: nil, updated_at: nil>
[2] pry(main)> Detail.where(:product_id => 1).to_a.each do |detail| j.job_values.build(:detail_id => detail.id) end
Detail Load (0.4ms) SELECT `details`.* FROM `details` WHERE `details`.`product_id` = 1
=> [#<Detail id: 1, name: "Size", product_id: 1, resource_id: nil, created_at: "2014-02-17 05:41:23", updated_at: "2014-02-17 05:41:23", required_at: "0", has_other: false, select_many: false, yes_no: false>,
#<Detail id: 2, name: "Colour", product_id: 1, resource_id: nil, created_at: "2014-02-17 05:41:23", updated_at: "2014-02-17 05:41:23", required_at: "0", has_other: true, select_many: false, yes_no: false>,
#<Detail id: 3, name: "Finishing", product_id: 1, resource_id: nil, created_at: "2014-02-17 05:41:23", updated_at: "2014-02-17 05:41:23", required_at: "0", has_other: false, select_many: true, yes_no: false>,
#<Detail id: 4, name: "Double Sided", product_id: 1, resource_id: nil, created_at: "2014-02-17 05:41:23", updated_at: "2014-02-17 05:41:23", required_at: "0", has_other: false, select_many: false, yes_no: true>,
#<Detail id: 5, name: "Rounded Corners", product_id: 1, resource_id: nil, created_at: "2014-02-17 05:41:23", updated_at: "2014-02-17 05:41:23", required_at: "2", has_other: false, select_many: true, yes_no: false>]
[3] pry(main)> j.job_values.second.chosen_property_ids = [5,3]
ChosenProperty Load (0.3ms) SELECT `chosen_properties`.* FROM `chosen_properties` WHERE `chosen_properties`.`id` IN (5, 3)
ActiveRecord::RecordNotFound: Couldn't find all ChosenProperties with IDs (5, 3) (found 0 results, but was looking for 2)
from /Users/egray/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/finder_methods.rb:198:in `raise_record_not_found_exception!'
[4] pry(main)>
class Job < ActiveRecord::Base
has_many :job_values
belongs_to :product
end
class JobValue < ActiveRecord::Base
belongs_to :job
belongs_to :detail
has_and_belongs_to_many :chosen_properties
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment