bcardarella
2007-Oct-17 22:11 UTC
has_many :through delete(object) only NULL foreign keys???
I have join model being used with the has_many :through relationship and am doing the following.... class Job has_many :views has_many :candidates_viewd, :through => :views, :source => :candidate end class Candidate has_many :views has_many :jobs_viewd, :through => :views, :source => :job end class View belongs_to :candidate belongs_to :job end def my_method user = Candidate.find(1) job = user.jobs_viewed.first user.jobs_viewed.delete(job) end When I was using has_and_belongs_to I could use the .delete(object) method and the record in the join table would be deleted. Now has_many :through delete(object) just NULLifies the foreign key. Is there a simple way around this or am I going to have to write custom methods to delete the specific join model record? BTW, for all the raving I''ve heard about has_many :through it seems to be much more work and less freedom than has_and_belongs_to (I was using push_with_attributes and that is being deprecated) --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Anthony Richardson
2007-Oct-17 22:28 UTC
Re: has_many :through delete(object) only NULL foreign keys???
bcardarella wrote:> def my_method > user = Candidate.find(1) > job = user.jobs_viewed.first > user.jobs_viewed.delete(job) > end >why not use: def my_method user = Candidate.find(1) user.jobs_viewed.first.destroy end --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bcardarella
2007-Oct-17 22:45 UTC
Re: has_many :through delete(object) only NULL foreign keys???
That just destroys the actual job. I want to delete the association in the job model. The work around I am using is ViewedJob.find_by_candidate_id_and_job_id(self.id, job.id).destroy But this is not a very elegant solution... it seems like I am losing some simple functionality with has_many :through On Oct 17, 6:28 pm, Anthony Richardson <anth...-fLWHnXb90il4ktuDPxIMMA@public.gmane.org> wrote:> bcardarella wrote: > > def my_method > > user = Candidate.find(1) > > job = user.jobs_viewed.first > > user.jobs_viewed.delete(job) > > end > > why not use: > > def my_method > user = Candidate.find(1) > user.jobs_viewed.first.destroy > end--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Oct-18 08:16 UTC
Re: has_many :through delete(object) only NULL foreign keys???
You need to set :dependant => :destroy on the :views association. Fred --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---