Hi I have problem with make working select_box with has_one association.
I have model image_element which is polymorphic:
class ImageElement < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
belongs_to :image
end
model image:
class Image < ActiveRecord::Base
attr_accessible :image, :application_id
belongs_to :application
has_many :image_elements, as: :imageable
mount_uploader :image, ImagesUploader
end
and model level which has got following association:
has_one :image_element, as: :imageable
has_one :image, through: :image_element
accepts_nested_attributes_for :image_element
In the level form I'm trying to create the select_box to select an image_element for level.
= f.select(:image_element, ImageElement.all.collect{|i| i.image.image.thumb})
Select box is viewing properly but when i submit the form i have the following output from the server:
WARNING: Can't mass-assign protected attributes: image_element
Thank's in advance :)
Try adding image_element_attributes to attr_accessible
attr_accessible :image, :application_id, :image_element_attributes
Related
I'm using a has many through pattern with these 3 models
class User < ActiveRecord::Base
has_many :user_topics
has_many :topics, through: :user_topics
end
class Topic < ActiveRecord::Base
validates_presence_of :name
validates :name, :uniqueness => true
end
class UserTopic < ActiveRecord::Base
belongs_to :user
belongs_to :topic
accepts_nested_attributes_for :topic
end
At the moment a new topic model is trying to be created every time a new user_topic is created. I'd like to create a new topic model only if the topic name doesn't already exist, otherwise if it does, use the existing topic_id.
So something like:
class UserTopic < ActiveRecord::Base
belongs_to :user
belongs_to :topic
accepts_nested_attributes_for :topic, :first_or_create(:name)
end
Is it possible to do something similar to this?
I've got the following order_params:
{"user_id":"1","order_status_id":"1","delivery_type_id":"1","delivery_time":"10","order_items":[{"count":"5","item_id":"1"}]}
These params are used for creating an Order object with nested order_item objects. Models:
class Order < ActiveRecord::Base
validates :user, :order_status, :delivery_type, presence: true
belongs_to :user
belongs_to :order_status
belongs_to :delivery_type
has_many :order_items
accepts_nested_attributes_for :order_items
end
Order_item:
class OrderItem < ActiveRecord::Base
validates :item, :order, :count, presence: true
belongs_to :item
belongs_to :order
end
But when I'm trying to create a new Order:
Order.create!(order_params)
I got the following error:
OrderItem(#70182455585540) expected, got ActiveSupport::HashWithIndifferentAccess(#70182454696760)
How can I fix it? Thanks in advance.
You key for order_items attributes is wrong, which should be order_items_attributes if you use nested attributes.
...,"order_items":[{"count":"5","item_id":"1"}]}
should be
...,"order_items_attributes":[{"count":"5","item_id":"1"}]}
I'm trying to save two models (one to many) in one time. My code looks like this:
#submission = Submission.new(submission_params)
#submission_asset = #submission.attachments.new(submission_asset_params)
#submission_asset.attachment_type = 'submission_asset'
if #submission.save
# render or redirect here
else
#submission.errors
end
But when I run this I get this error #messages={:attachments=>["is invalid"]}. I think it's because my attachment model has this:
# Attachment model snippet
validates :attachable_id, :attachable_type, presence: true
But it's to ensure it is attached to a Submission. But when I remove or comment out the validation it works and saves the two models and the association.
How do I make this save?
EDIT
class Submission < ActiveRecord::Base
has_many :attachments, as: :attachable, dependent: :destroy
end
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
validates :attachable_id, :attachable_type, presence: true
end
#submission_asset = #submission.attachments.build(submission_asset_params)
UPDATE:
class Submission < ActiveRecord::Base
has_many :attachments, as: :attachable, inverse_of: :attachable, dependent: :destroy
end
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
validates :attachable, presence: true
end
I'm struggle to find a way to have more than one property of the same model. I've this structure :
class Image < ActiveRecord::Base
attr_accessible :name, :content_type
end
class MenuImage < ActiveRecord::Base
belongs_to :image
belongs_to :menu
end
class Menu < ActiveRecord::Base
has_many :menu_images
has_many :images, :through => :menu_images
has_one :image, :as => :thumbnail_image
attr_accessible :thumbnail_image_id
end
I want to be able to access the images using #menu.images and #menu.thumbnail_image.
The code has_one :image, :as => :thumbnail_image is an example of what i'm trying to obtain.
You name it differently, but tell ActiveRecord to use the Image class:
has_one :thumbnail_image, :class_name => "Image"
See also the Association Guide: http://guides.rubyonrails.org/association_basics.html#has_one-association-reference
I'm creating trips in my rails app and i want to add categories to those trips. The categories are stored in the Categories table in my DB and the user may select which categories are suitable for the trip. So multiple categories a trip can be used.
Although i'm a noob i figured some things out with some help of the RoR guides about this subject. Now, i've got a 3rd table tripcategories which will have to hold the trip_id and the category_id. Right? With that i've got the following models:
trip.rb:
class Trip < ActiveRecord::Base
attr_accessible :description, :title, :user_id, :triplocations_attributes, :photo
has_many :triplocations, :dependent => :destroy
has_many :tripcategories
has_many :categories, :through => :tripcategories
accepts_nested_attributes_for :triplocations, allow_destroy: true
end
category.rb:
class Category < ActiveRecord::Base
attr_accessible :name
has_many :tripcategories
belongs_to :trip, :through => :tripcategories
end
tripcategory.rb:
class Tripcategory < ActiveRecord::Base
attr_accessible :category_id, :trip_id
belongs_to :trip
belongs_to :category
end
When i'm trying it this way and trying to call trip.categories in my trip index it says "Unknown key: through". Am i doing something horribly wrong or am i missing the bigger picture?
Thanks in advance!
class Category < ActiveRecord::Base
attr_accessible :name
has_many :tripcategories
has_many :trips, :through => :tripcategories
end