On Aug 11, 5:53 pm, Frederick Cheung <[email protected]>
wrote:
> why not do the assignment from the before_create callback ? I'm not
> entirely sure why you're trying to do this .

I guess what I'm saying is that I'm not sure how to do this. Here's
what I'm thinking that I'd like to do in terms of order of operations:

ImagesController::create
   - accepts the form input. This includes the uploaded file since the
Image model has an accessor attribute (:upload).
   - instantiates a new image with the passed params.
   - calls the Image model's save method

Image
   - calls the Binary model to upload the physical file and set/store
its properties
   - sets its own binary_id property based on the return value from
the call to Binary::upload
   - saves itself

Maybe there's a more efficient way to do this than how I'm thinking?
If not, I guess what I'm not sure about is how to make that all work
together most efficiently. There are several different callback
mechanisms in Rails and I don't have a good enough feel for which is
most appropriate in this scenario. I'm thinking of something like this
in the Image model:

class Image < ActiveRecord::Base
  belongs_to :binary

  validates_presence_of( :upload )

  before_create :upload_physical_file

  attr_accessor :upload

  protected

  def upload_physical_file
    @binary = self.binary.upload( :upload )
    self.binary_id = @binary.id
  end
end

I have an unexpected nil object that I'll debug, but I don't have a
good feel that I'm doing this the best way. Being completely new to
Ruby and Rails, I don't have a good feel for how to access related
models or what objects are available at which times. For example, I
don't know whether I have to explicitly instantiate a Binary object in
my Image model or, since they're related, I can assume one is
instantiated and access it as self.binary.

At this point, I'm fighting the learning curve and may not even be
asking the right questions. Any advice you can offer based on either
the scenario or the posted code would be much appreciated.

Thanks again.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to