Forum: Ruby on Rails Active Resource basics

Posted by bryanl (Guest)
on 2007-01-09 03:59
(Received via mailing list)
Assuming I have models that look like these:

class Job < ActiveRecord::Base
  has_many :locations
end

class Location < ActiveRecord::Base
  belongs_to :job
end

I have routes that look like this:

map.resources :job do |job|
  job.resources :location
end

I'm exposing these model using ActiveResource like this:

class Job < MyLocalResource
end

class Location < MyLocalResource
  site += '/job/:job_id'
end

How would I run a method on job, (like submitting the job to queue of
some sort) after I create location?  Am I approaching this from the
wrong direction?  Should I create an observer on the job model?
Posted by Jeremy Kemper (Guest)
on 2007-01-09 06:08
(Received via mailing list)
On 12/31/06, bryanl <bryanliles@gmail.com> wrote:
>
>   site += '/job/:job_id'
> end
>
> How would I run a method on job, (like submitting the job to queue of
> some sort) after I create location?  Am I approaching this from the
> wrong direction?  Should I create an observer on the job model?


Active Resource doesn't reflect the associations in your models, but you
can, for example:

# Location
  def job
    @job ||= Job.find(job_id)
  end

# Job
  def enqueue
    connection.post("#{element_path(id)};enqueue")
  end

Then location.job.enqueue! to submit the location's job to a queue.

jeremy
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.