Rails model multiple properties of the same model - ruby-on-rails

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

Related

Paperclip separate styles

How can i separate styles for different models?
I have my Photo model which generate my styles
large:'300x300#',huge:'800x800'
also i have two models witch use this styles
Product
and
Post
so i want to use
large style only for Product
and huge style only for Post
product => has_many :photos
post => has_one :photo
photo => belongs_to :post
belongs_to :product
has_attached_file :image, :styles => {large:'300x300#',huge:'800x800'}
Is it possible?
I advice you to separte image by a image model type using STI, and polymorphism, therefore, add imageable_type, imageable_id, and type field to photo model, so:
app/models/photo.rb:
class Photo < AR::Base
belongs_to :imageable, polymorphic: true
end
app/models/photos/large_photo.rb:
class LargePhoto < Photo
has_attached_file :image, :styles => { large:'300x300#' }
end
app/models/photos/huge_photo.rb:
class HugePhoto < Photo
has_attached_file :image, :styles => { huge:'800x800' }
end
app/models/product.rb:
class Product < AR::Base
has_many :large_photos, as: imageable
end
app/models/post.rb:
class Post < AR::Base
has_one :huge_photo, as: imageable
end
BUT for me, will be better to use carrierwave, rather than paperclip for this case.

has_one polymorphic association select_box

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

Association through another table "Unknown key: through"(Rails)

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

Polymorphic has_many: through in rails

I have a tags model that I'd like to be polymorphic, but I don't want five records for a tag of "video" for example, I want to create the tag once and be able to use it on a variety of models. I've ready some of the questions here about doing that, but I'm not quite getting how to make it work.
So I've got:
class Tag < ActiveRecord::Base
belongs_to :tagable, :polymorphic => true
end
and
class Post < ActiveRecord::Base
has_many :tags, :through => :tag_assignments
end
and
class TagAssignment < ActiveRecord::Base
has_many :tags, :as => :taggable
end
Seems to me that should work, but... reading all the questions here I know I need a :source => option in there somewhere to tie it all together, but I'm just not following exactly how to do it. Can anyone help?
You have to redo your models as follows:
class Tag < ActiveRecord::Base
has_many :tag_assignments
end
class TagAssignment < ActiveRecord::Base
belongs_to :tagable, :polymorphic => true
belongs_to :tag
end
class Post < ActiveRecord::Base
has_many :tag_assignments, :as => :tagable
has_many :tags, :through => :tag_assignments
end
Now given a post you can get its tags as follows:
post.tags
Note
You should consider using the acts-as-taggable-on gem for your use case.

How can I make a polymorphic connection be dependent?

I am trying to make my image_maps get destroyed when either a product or image is deleted. Here is the code.
class ImageMap < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
belongs_to :image
end
class Product < ActiveRecord::Base
has_many :image_maps, :as => :imageable
has_many :images, :through => :image_maps
end
class Image < ActiveRecord::Base
has_many :image_maps, :as => :imageable, :dependent => :destroy
end
Right now the image_maps do not get destroyed when you delete an image and i still need to figure out how to get it to work for products too.
I think that your dependent should go to the ImageMap model.
If i remember correctly, this is what Rails checks on the associated models to see whether they need to be destroyed.

Resources