jayD
jayD

Reputation: 819

ruby on rails Instantiating inside a model?

I'm somehow newly intermediate(:D ) in rails and i'm starting to tackle more complex project that require multiple class and interaction with my model and i'm kind of lost in how to design/order my code.

I have a product_table and a product_details_table.

Every time a product is created an image is uploaded with it.

In the class Product , i have created a few methods that populate virtual attributes for that product related to that image ( size , etc. ). This is all working with paperclip callback after upload.

My issue is that from that image size i would like to automatically generate attributes values in my product_details table.

Product_details.new(product_id:current_product_id(**is it self.id here?**),size:product.virtual_attribut_size,x:virtual_attribut_x)

How would you do that ?

I would have done it in my controller the thing is it has to been done automatically after the file upload and not before and i don't know how to do that.

If i do it in my model i'm guessing it can work ( as a normal class ) but is that the way to do it ?

Thanks to those who try to help

edit :

Basically my Product model would look like this :

class Product < ActiveRecord::Base
  def image_to_inch
    #return "30x30" (in inch) for an image uploaded in pixel (divide      the number of pixel by 300dpi to get a good quality print )
  end
  def image_printable_size
    #use previous method result to output an array of all printable size from a list of predifined sizes. example : 30x30 can be printed in 30x30,20x20,10x10 but not 40x40.
    #output ["30x30","20x20","10x10"]
  end

##Here i should iterate over the array and create a product_details line for that product for each size :

   ## simplified version of what i was going for and that look really really ugly :
  ["30x30","20x20","10x10"].each do |size|
    ProductDetail.create!(product_id:self.id,size:size)
  end
end

i've left out the callbacks,validation,etc. so that it's easier to read.

Upvotes: 0

Views: 625

Answers (1)

Max Williams
Max Williams

Reputation: 32933

Your requirements aren't clear, but here's some strategy tips.

  1. Use before_save or after_save callbacks to automate code.
  2. Use attr_accessor variables to hold temporary objects which are used by before_save and after_save callbacks
  3. Make simple methods to do simple things. Remember that you can write your own custom getter and setter methods.

So, your approach could be something like this: i'm guessing at your schema so don't get too hung up on the details.

class Product
  has_one :product_detail
  after_save :update_product_details

  def update_product_detail
    product_detail = self.product_detail || self.product_detail.build
    if self.image
      product_detail.update_from_image(self.image)
    end
    product.save
  end

class ProductDetail
  belongs_to :product

  def update_from_image(image)
    self.size = image.size
    #... any other settings taken from the image
  end

Upvotes: 1

Related Questions