I've got two tables, User and Allergy. These are connected via another table, UserAllergy. The models are as would be expected:
class User
has_many :user_allergies
has_many :allergies, through: :user_allergies
end
class UserAllergy
belongs_to :user
belongs_to :allergy
end
class Allergy
has_many :user_allergies
has_many :users, through :user_allergies
end
What I'm confused about is creating allergies from a multiple-valued collection_select in my User form.
I have the following field:
<%= f.collection_select :allergy_ids,
Allergy.all,
:id,
:name,
{},
{ class: 'form-control', multiple: true }
%>
This correctly inserts a key into my params like so if I selected the Allergies with ids 1 and 2:
{ user: { id: "1", allergy_ids: ["", "1", "2"] } }
When I create the user instantiated with #user = User.new( my_params ), the weird behavior occurs. Instead of inserting the provided allergy_ids into the join table, Rails does a query to get all current user_allergies for the user, then deletes all of the current user_allergies:
Started PATCH "/employees/regular_user" for 127.0.0.1 at 2015-06-18 22:08:30 -0400
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "user"=>{ "allergy_ids"=>["", "1", "2", "3"]}, "button"=>"", "id"=>"regular_user"}
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
(0.1ms) begin transaction
Allergy Load (0.1ms) SELECT "allergies".* FROM "allergies" INNER JOIN "user_allergies" ON "allergies"."id" = "user_allergies"."allergy_id" WHERE "user_allergies"."user_id" = ? [["user_id", 1]]
SQL (0.1ms) DELETE FROM "user_allergies" WHERE "user_allergies"."user_id" = ? AND "user_allergies"."allergy_id" = 1 [["user_id", 1]]
(27.4ms) commit transaction
Redirected to http://localhost:3000/employees/regular_user
Completed 302 Found in 32ms (ActiveRecord: 27.8ms)
Anyone knows what gives, or what I need to do to create allergies implicitly? I've tried accepts_nested_attributes_for and changing around the form to use fields_for.
So, I went and looked at code of mine that does a similar function. Here's what my create method looks like. This is creating a Student with assignment to Student Groups in a school setting (I didn't use "class" since Ruby wouldn't like that).
def create
#student = Student.new(student_params)
if #student.save
#student.student_groups = StudentGroup.where(id: params[:student][:student_group_ids])
flash[:success] = "Student was successfully created."
redirect_to #student
else
render 'new', notice: "Your student could not be created."
end
end
I completely ignore the Student Group IDs when creating the student_params, since I'm not using them for mass assignment.
Yes, one extra line of code. I'd be really interested to hear if there's a way to accomplish this via mass assignment.
You're missing one part of the puzzle which is the relation from Allergy to User.
class Allergy
has_many :user_allergies
has_many :users, through: :user_allergies
end
Just give the following code a try-
params.require(:user).permit(___, ____, {allergy_ids: []}, ____, ____)
Related
I am trying to modify my image upload routine in order to upload multiple images at the same time. Currently I am able to upload multiple images, but it creates only one record. How can I achieve it that one submit creates multiple records?
I do not want to go through an association, but instead be able to directly upload multiple images.
I currently have a Model:
class Diapo < ApplicationRecord
belongs_to :album
mount_uploaders :file_name, DiapoUploader
end
My Controller:
class Admin::DiaposController < ApplicationController
def new
#diapo = Diapo.new
#page_title = 'Create new Diapo'
end
def create
#diapo = Diapo.new(diapo_params)
if #diapo.save
flash[:notice] = 'Diapo #{#diapo.file_name} was successfully created.
redirect_to action: :index
else
#page_title = 'Create new Diapo'
render action: :new
end
end
.
.
.
private
def diapo_params
params.require(:diapo).permit({file_name: []}, :title, :alt, :author, :copyright, :album_id)
end
end
in the form I have:
<%= f.file_field :file_name, multiple: true, class: 'form-field' %>
Currently I get one post
Started POST "/admin/diapos" for ::1 at 2019-12-18 10:45:19 +0100
Processing by Admin::DiaposController#create as HTML
Parameters: {"authenticity_token"=>"something==", "diapo"=>{"file_name"=>[#<ActionDispatch::Http::UploadedFile:0x00007f9062b51a40 #tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20191218-2329-7kffxo.jpg>, #original_filename="1.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"diapo[file_name][]\"; filename=\"1.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x00007f9062b519f0 #tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20191218-2329-o5qzhe.jpg>, #original_filename="2.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"diapo[file_name][]\"; filename=\"2.jpg\"\r\nContent-Type: image/jpeg\r\n">], "alt"=>"Test", "author_ids"=>"", "album_id"=>"124", "copyright"=>"Test"}, "commit"=>"Save"}
Admin Load (0.4ms) SELECT 'admins'.* FROM 'admins' WHERE 'admins'.'id' = 1 ORDER BY 'admins'.'id' ASC LIMIT 1
Unpermitted parameter: :author_ids
(0.2ms) BEGIN
↳ app/controllers/admin/diapos_controller.rb:14:in `create'
Album Load (0.3ms) SELECT `albums`.* FROM `albums` WHERE `albums`.`id` = 124 LIMIT 1
↳ app/controllers/admin/diapos_controller.rb:14:in `create'
Diapo Create (0.2ms) INSERT INTO `diapos` (`file_name`, `alt`, `copyright`, `album_id`, `created_at`, `updated_at`) VALUES ('[\"1.jpg\", \"2.jpg\"]', 'Test', 'Test', 124, '2019-12-18 09:45:34.406410', '2019-12-18 09:45:34.406410')
↳ app/controllers/admin/diapos_controller.rb:14:in `create'
(6.5ms) COMMIT
↳ app/controllers/admin/diapos_controller.rb:14:in `create'
Redirected to http://localhost:3000/admin/diapos
Completed 302 Found in 15350ms (ActiveRecord: 7.5ms | Allocations: 75512)
and 1 record is created which has the array of filenames in the filename field.
I suspect I can do it either in the controller or through javascript? How would I do this?
Thank you in advance!
You have to implement your requirement with has_many relation.
To save multiple record you may have parent child relationship just like below.
Ex :-
Post is a parent table and save its images as post_attachments which is child table
class Post < ActiveRecord::Base
has_many :post_attachments
accepts_nested_attributes_for :post_attachments
end
class PostAttachment < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :post
end
this is how you can achieve to save multiple files for parent record
For more detail see this Article which may be helpful to you.
https://kolosek.com/carrierwave-upload-multiple-images/
Rails 5.2
I have a partial:
views/authors/_add_author_comment.html/slim
= form_for :author_note, url: author_notes_url, method: :post do |f|
= f.hidden_field :author, value: #book.author
= f.text_area :comment
button.btn.btn-primary type="button"
= f.submit t('authors.show.submit_comment')
In my controllers/author_notes_controller.rb, I have:
def create
#author_note = AuthorNote.new(author: params[:author_note][:author], user_id: current_user.id, comment: params[:author_note][:comment])
#author_note.save
end
When the form displays (part of a larger view), and I fill the comment out, and click on "Submit Comment", the comment is not saved. In the console, I see the following:
Processing by AuthorNotesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Jju1cpsLjXLY/TaF9p/Zkh8JQ/+KajjxwQHgNU4tNU9bjL8BiZQ8xL3S7ske1KqflOPHVaB9UTWRvgxNqzLd7Q==", "author_note"=>{"author"=>"John Dow", "comment"=>"This is a test"}, "commit"=>"Save Note"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = '5' ORDER BY `users`.`id` ASC LIMIT 1
↳ app/controllers/author_notes_controller.rb:23
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = '5' LIMIT 1
↳ app/controllers/author_notes_controller.rb:23
(0.2ms) BEGIN
↳ app/controllers/author_notes_controller.rb:28
(0.1ms) ROLLBACK
↳ app/controllers/author_notes_controller.rb:28
No template found for AuthorNotesController#create, rendering head :no_content
Completed 204 No Content in 60ms (ActiveRecord: 10.6ms)
Why is it ActiveRecord rolling back, and not saving the note to the author_notes table?
Resolution:
the author_note.rb model, I had: belongs_to :book, I commented it out
You didn't include your AuthorNote model, there might be some validation constraints that prevents the author_note from being saved.
Your code also doesn't handle validation error, so you might want to do that. But you can simply check for errors like:
def create
#author_note = AuthorNote.new(author: params[:author_note][:author], user_id: current_user.id, comment: params[:author_note][:comment])
#author_note.save
# puts works too
logger.debug "author_note save error: #{#author_note.errors.full_messages.join(' ')}"
end
This is highly likely due to rails 5 making belongs_to association required by default.
What that means is from rails > 5, if you define belongs_to on a model and if the corresponding record is not present. In this case, book_id should be nil in the AuthorNote record. ActiveRecord will error it out and rollback the transaction.
To fix this, instead of commenting out the belongs_to relationship all together, you can make it optional (beacause, removing the relationship might break the system)
class AuthorNote < ApplicationRecord
belongs_to :book, optional: true
end
Use ! along with the save or create that will show the error message Object.save! or Object.create!
def create
#author_note = AuthorNote.new(author: params[:author_note][:author], user_id: current_user.id, comment: params[:author_note][:comment])
#author_note.save!
end
OR
def create
#author_note = AuthorNote.create!(author: params[:author_note][:author], user_id: current_user.id, comment: params[:author_note][:comment])
end
I'm working on a Rails 4 engine and I have a simple has_many through association that is being created correctly but seems to break when I try to remove the association.
I'm using Rails 4, Formtastic and Cocoon. Here are my models and associations.
# Role
module Kohcms
class Role < ActiveRecord::Base
has_many :permission_roles, dependent: :destroy
has_many :permissions, through: :permission_roles
accepts_nested_attributes_for :permissions, reject_if: proc { |attributes| attributes[:subject_class].blank? }, allow_destroy: true
end
end
# Permission
module Kohcms
class Permission < ActiveRecord::Base
has_many :permission_roles, dependent: :destroy
has_many :roles, through: :permission_roles
end
end
# Permission Role Join Model/Table
module Kohcms
class PermissionRole < ActiveRecord::Base
belongs_to :role
belongs_to :permission
end
end
When I add the new association, it works fine. But when I delete it, I get this error:
ActiveRecord::StatementInvalid in Kohcms::Admin::RolesController#update
Mysql2::Error: Unknown column 'kohcms_permission_roles.' in 'where clause': DELETE FROM `kohcms_permission_roles` WHERE `kohcms_permission_roles`.`` = NULL
Here is an output:
Started PATCH "/admin/roles/4" for 127.0.0.1 at 2013-07-20 14:46:11 -0500
Processing by Kohcms::Admin::RolesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Dnj2RDxlK7XJTf6NZLgmuIQCDOVfjhWjsN1mCPpHIn4=", "commit"=>"Update Role", "role"=>{"title"=>"asdfadsfadf", "permissions_attributes"=>{"0"=>{"_destroy"=>"1", "name"=>"asdf", "subject_class"=>"ActiveRecord::SchemaMigration", "subject_id"=>"", "action"=>"All", "id"=>"16"}}, "full_access"=>"0", "canlock"=>"0", "user_ids"=>[""]}, "id"=>"4"}
Kohcms::User Load (0.3ms) SELECT `kohcms_users`.* FROM `kohcms_users` WHERE `kohcms_users`.`id` = 1 ORDER BY `kohcms_users`.`id` ASC LIMIT 1
Kohcms::Role Load (0.3ms) SELECT `kohcms_roles`.* FROM `kohcms_roles` WHERE `kohcms_roles`.`id` = 1 ORDER BY `kohcms_roles`.`id` ASC LIMIT 1
Kohcms::Role Load (0.2ms) SELECT `kohcms_roles`.* FROM `kohcms_roles` WHERE `kohcms_roles`.`id` = 4 LIMIT 1
(0.1ms) BEGIN
Kohcms::User Load (0.3ms) SELECT `kohcms_users`.* FROM `kohcms_users` WHERE `kohcms_users`.`role_id` = 4
Kohcms::Permission Load (0.3ms) SELECT `kohcms_permissions`.* FROM `kohcms_permissions` INNER JOIN `kohcms_permission_roles` ON `kohcms_permissions`.`id` = `kohcms_permission_roles`.`permission_id` WHERE `kohcms_permission_roles`.`role_id` = 4 AND `kohcms_permissions`.`id` IN (16)
Kohcms::Role Exists (0.3ms) SELECT 1 AS one FROM `kohcms_roles` WHERE (`kohcms_roles`.`title` = BINARY 'asdfadsfadf' AND `kohcms_roles`.`id` != 4) LIMIT 1
Kohcms::PermissionRole Load (0.2ms) SELECT `kohcms_permission_roles`.* FROM `kohcms_permission_roles` WHERE `kohcms_permission_roles`.`role_id` = 4 AND `kohcms_permission_roles`.`permission_id` = 16
SQL (0.3ms) DELETE FROM `kohcms_permission_roles` WHERE `kohcms_permission_roles`.`` = NULL
Mysql2::Error: Unknown column 'kohcms_permission_roles.' in 'where clause': DELETE FROM `kohcms_permission_roles` WHERE `kohcms_permission_roles`.`` = NULL
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 11ms
Thanks in advance!
EDIT
Here is the update method. It's a shared method so my Roles and Permissions controllers inherit from another controller with this:
def update
#parent = parent_model
#parents = parent_models
#model = fetch_model
#model = pre_update(#model)
if #model.errors.empty? and #model.update_attributes(permitted_params)
message = "#{#model.class.name.demodulize.titlecase} was successfully updated."
# allows for some basic controler specific functionality without redefining the create method
succeeding_update(#model)
respond_to do |format|
format.html {
if params[:redirect_to].present?
redirect_to params[:redirect_to], notice: message
else
redirect_to edit_model_link(#model), notice: message
end
}
format.json {
render_json_model_response #model, message, 'updated'
}
end
else
flash[:error] = 'There was an error, please try again.'
respond_to do |format|
format.html {
if params[:redirect_to].present?
redirect_to params[:redirect_to], notice: message
else
redirect_to edit_model_link #model
end
}
format.json { render_json_response :error, :notice => 'There was an error, please try again' }
end
end
end
You need to add an ID column to your PermissionRole table. I updated my association from HABTM (where you don't need an ID), and found that it was causing exactly the same problem.
This migration should do (obviously generate migration & change to your specs):
add_column :image_products, :id, :primary_key
This should resolve your issue if the relevant dependent: :destory elements are in place
I Having an issue when trying to update a model that has has_and_belongs_to_many association.
Let's say that Post has_and_belongs_to_many Tag, and Post validates the presence of title and Tags.
If I update Post, removing its title and tags, I get validation error in title and tags, ok.
But ActiveAdmin already removed the records that make association between Post and Tag, so, if I leave Post edit page, the post is left invalid on database, without tags.
Here my models:
class Tag < ActiveRecord::Base
attr_accessible :label
has_and_belongs_to_many :posts
end
class Post < ActiveRecord::Base
attr_accessible :content, :title, :tag_ids
has_and_belongs_to_many :tags
validates_presence_of :content, :title, :tags
end
ActiveAdmin.register Post do
form do |f|
f.inputs do
f.input :title
f.input :content
f.input :image
f.input :tags
end
f.buttons
end
end
I usign chosen-rails gem and it allows user to unselect all tags of post.
Summarizing, my problem is: ActiveAdmin updates relationships on database before perform model validations.
There a solution for this behavior or I doing something wrong?
Edit:
Here the request log when I trying to update post without title and tags:
Started PUT "/admin/posts/8" for 127.0.0.1 at 2013-04-01 10:32:07 -0300
Processing by Admin::PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"amSbLlP/rgDrNn/N8lgq/KEaRXK1fMPShZDwpZ0QIJ4=", "post"=>{"title"=>"", "content"=>"content", "tag_ids"=>["", ""]}, "commit"=>"Update Post", "id"=>"8"}
AdminUser Load (0.2ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1 LIMIT 1
Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1
Tag Load (0.2ms) SELECT `tags`.* FROM `tags` INNER JOIN `posts_tags` ON `tags`.`id` = `posts_tags`.`tag_id` WHERE `posts_tags`.`post_id` = 8
(0.1ms) BEGIN
SQL (12.3ms) DELETE FROM `posts_tags` WHERE `posts_tags`.`post_id` = 8 AND `posts_tags`.`tag_id` IN (1, 2)
(49.6ms) COMMIT
(0.1ms) BEGIN
(0.2ms) ROLLBACK
Post Load (0.3ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1
Tag Load (0.2ms) SELECT `tags`.* FROM `tags`
Rendered /home/rodrigo/.rvm/gems/ruby-1.9.3-p125#blog/gems/activeadmin-0.5.1/app/views/active_admin/resource/edit.html.arb (192.3ms)
Completed 200 OK in 276ms (Views: 194.8ms | ActiveRecord: 63.3ms)
EDIT 2:
Ok, I sure that ActiveAdmin has this bug.
Looking at ActiveRecord behaviour, I think that validation flow is broken using only model class. See this example:
1.9.3p125 :064 > post = Post.find(8)
Post Load (0.3ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1
=> #<Post id: 8, title: "title", content: "content", created_at: "2013-03-27 13:13:20", updated_at: "2013-03-27 13:13:20", image: "extrato.bmp">
1.9.3p125 :065 > post.tags
Tag Load (0.2ms) SELECT `tags`.* FROM `tags` INNER JOIN `posts_tags` ON `tags`.`id` = `posts_tags`.`tag_id` WHERE `posts_tags`.`post_id` = 8
=> [#<Tag id: 1, label: "tag", created_at: "2013-02-25 18:32:45", updated_at: "2013-02-25 18:32:45">, #<Tag id: 2, label: "new", created_at: "2013-02-25 18:32:50", updated_at: "2013-02-25 18:32:50">]
1.9.3p125 :066 > post.title = ""
=> ""
1.9.3p125 :067 > post.save #<<<<<<< It's invalid on title
=> false
1.9.3p125 :068 > post.tags = [] #<<<<<<< This shouldnt trigger database update
(0.3ms) BEGIN
SQL (0.5ms) DELETE FROM `posts_tags` WHERE `posts_tags`.`post_id` = 8 AND `posts_tags`.`tag_id` IN (1, 2)
(55.5ms) COMMIT
=> []
1.9.3p125 :069 > post.save #<<<<<<< It's invalid on title AND TAGS
(0.2ms) BEGIN
(0.2ms) ROLLBACK
=> false
1.9.3p125 :070 > post.reload
Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1
=> #<Post id: 8, title: "title", content: "content", created_at: "2013-03-27 13:13:20", updated_at: "2013-03-27 13:13:20", image: "extrato.bmp">
1.9.3p125 :071 > post.valid? #<<<<<<< Now, I have this model in invalid state
Tag Load (0.6ms) SELECT `tags`.* FROM `tags` INNER JOIN `posts_tags` ON `tags`.`id` = `posts_tags`.`tag_id` WHERE `posts_tags`.`post_id` = 8
=> false
Has any way to update post attributes(including tags) and validate the model before doing any database update?
You can use this gem: https://github.com/MartinKoerner/deferred_associations
The deferred associations will fix this bug.
#Rodrigo I was able to reproduce your issue locally without active admin, the issue is actually that is one of the default operations when using HABTM relationships if you see [here][1]
[1]: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many it says:
collection=objects
Replaces the collection’s content by deleting and adding objects as appropriate.
So apparently you will need to override this operation
Here is an example:
Override ActiveRecord << operator on has_many :through relationship, to accept data for the join model
Let me know how can I help you
I'd propose to add a tmp variable and store values in it. Then you should move them to database if validation passed.
Here is an example:
Your models/article.rb:
class Article < ActiveRecord::Base
validate :author_presence
has_and_belongs_to_many :authors
attr_writer :tmp_author_ids
def tmp_author_ids
#tmp_author_ids || author_ids
end
def author_presence
if tmp_author_ids.reject(&:blank?).empty?
errors.add(:tmp_author_ids, 'Author is missing')
else
self.author_ids = tmp_author_ids
end
end
end
Your admin/article.rb, block form:
f.input :tmp_author_ids, as: :select, multiple: true, collection: Author.all, label: 'Authors'
That's it
So, for anyone having the same issue, I found a workaround:
You can define a before_remove in the has_and_belongs_to_many association and raise an exception that then you can catch in ActiveAdmin.
For this it would be
class Post < ActiveRecord::Base
attr_accessible :content, :title, :tag_ids
has_and_belongs_to_many :tags, before_remove: :check_something
validates_presence_of :content, :title, :tags
end
def check_something(agent)
if self.tags.size == 1
raise ActiveModel::MissingAttributeError.new 'something'
end
end
more info: https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
if you want, you can redefine you update action in active_admin to prevent saving empty tags, in something like this style
ActiveAdmin.register Post do
controller do
def update
if params[:post][:tag_ids] == ["", ""]
flash.now[:alert] = "You can't remove all tags"
render :edit
else
super
end
end
end
...
end
and i think this stuff from model can be deleted
attr_accessor :new_tag_ids
validate :validate_new_tags_ids
after_save :update_tags
def update_tags
self.tag_ids = #new_tag_ids if defined?(#new_tag_ids)
#new_tag_ids = nil
end
private
def validate_new_tags_ids
errors[:tags] << "can't be blank (2)" if #new_tag_ids.blank?
end
Apologies in advance for another newbie question, but the ROR syntax is just not clicking with me, I can't get my head around the shortcuts and conventions (despite reading a couple of books already!) -
I effectively copied this from a book, but I"m trying to work out what is build, create etc?
#cart = current_cart
product = Catalog::Product.find(params[:product_id])
Rails.logger.debug { "Value in cart id " + #cart.id.to_s }
#checkout_line_item = #cart.line_items.build(product: product)
respond_to do |format|
if #checkout_line_item.save...
The output from log is this:
Processing by Checkout::LineItemsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9NH+xgDPTf/iN7RCdPd8H9rAIqWsSVB/f/rIT++Kk7M=", "product_id"=>"7"}
Created a line item
(0.1ms) BEGIN
SQL (2.0ms) INSERT INTO `checkout_carts` (`created_at`, `discounts`, `grand_total`, `loyalty_points`, `order_date`, `subtotal`, `timestamps`, `total_tax`, `updated_at`) VALUES ('2012-08-21 11:06:15', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2012-08-21 11:06:15')
(0.2ms) COMMIT
Catalog::Product Load (0.2ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 7 LIMIT 1
Value in cart id 8
Completed 500 Internal Server Error in 5ms
NoMethodError (undefined method `save' for nil:NilClass):
app/controllers/checkout/line_items_controller.rb:55:in `block in create'
app/controllers/checkout/line_items_controller.rb:54:in `create'
I'm guessing the problem lies with the build syntax where it builds the checkout line item, or possibly I've set up the has_many associations wrong. Is this enough for someone to help me troubleshoot? Or should I post the model declarations?
Update with models:
class Checkout::LineItem < ActiveRecord::Base
attr_accessible :customer_update_date, :inventory_status, :line_item_color, :line_item_description, :line_item_size, :line_item_tagline, :line_item_total, :quantity, :sku_id, :style_id, :tax, :tax_code, :timestamps, :unit_price, :product
belongs_to :cart
belongs_to :product, :class_name => 'Catalog::Product'
end
class Checkout::Cart < ActiveRecord::Base
attr_accessible :discounts, :grand_total, :loyalty_points, :order_date, :subtotal, :timestamps, :total_tax
has_many :line_items, dependent: :destroy
end
module Catalog
class Product < ActiveRecord::Base
attr_accessible :assoc_product,:product_id, :merch_associations, :aux_description, :buyable, :long_description, :name, :on_special, :part_number, :release_date, :short_description, :withdraw_date, :occasion
<<clipped for brevity>>
has_many :line_items, :class_name => 'Checkout::LineItem'
...
end
Can't answer my own question, but I think I got the answer:
It looks like I needed to add the cart to the build call...
this appears to have worked (I think, there's another blocking problem, but I can sort that one):
#cart = current_cart
product = Catalog::Product.find(params[:product_id])
Rails.logger.debug { "Value in cart id " + #cart.id.to_s }
#checkout_line_item = #cart.line_items.build(product: product, cart: #cart)
Build essentially creates and reserves space for a blank object. This allows you to create an associated object without saving it. The only use case I've experienced this with is a nested form where I had multiple dates for an event, so I used 5.times.build to create 5 empty date associations.