here is my code class Manufacture < ActiveRecord::Base belongs_to :item before_save :set_has_manufacture_yes before_destroy :set_has_manufacture_no def set_has_manufacture_yes self.item.has_manufacture = "Yes" end def set_has_manufacture_no self.item.has_manufacture = "No" end For some reason the value is not getting set. Is this because I am trying to set a value in the ITEMS table and not MANUFACTURES? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-May-22 00:52 UTC
Re: how to set a data value before saving or destroying
What is this has_manufacture field and why is it so important? On Thu, May 22, 2008 at 10:21 AM, Scott Kulik < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > here is my code > > class Manufacture < ActiveRecord::Base > belongs_to :item > before_save :set_has_manufacture_yes > before_destroy :set_has_manufacture_no > > def set_has_manufacture_yes > self.item.has_manufacture = "Yes" > end > > def set_has_manufacture_no > self.item.has_manufacture = "No" > end > > For some reason the value is not getting set. Is this because I am > trying to set a value in the ITEMS table and not MANUFACTURES? > -- > Posted via http://www.ruby-forum.com/. > > > >-- Appreciated my help? Recommend me on Working With Rails http://workingwithrails.com/person/11030-ryan-bigg --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> What is this has_manufacture field and why is it so important? > > On Thu, May 22, 2008 at 10:21 AM, Scott Kulik < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> end >> > >> > > > -- > Appreciated my help? > Recommend me on Working With Rails > http://workingwithrails.com/person/11030-ryan-biggbasically when someone create a manufacture i have a drop down list of all the items that don''t already have a manufacture. so what i''m trying to do is an easy way to populate the drop down list with all the items that don''t have a manufacture yet. here a few fields from my data structures ITEMS id name has_manufacture ... MANUFACTURES id item_id ... so my collection select currently selects all items where has_manufacture = "No" i''m not sure if there is another way i could do something like select NOT items.manufacture.blank? i''m sure there is probably an easier way to do it but i''m not sure of the logic. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
oops i forgot to mention my models ITEMS has_one :manufacture MANUFACTURES belongs_to :item -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-May-22 01:16 UTC
Re: how to set a data value before saving or destroying
is has_manufacture a boolean field? On Thu, May 22, 2008 at 10:41 AM, Scott Kulik < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > oops i forgot to mention my models > > ITEMS > has_one :manufacture > > MANUFACTURES > belongs_to :item > -- > Posted via http://www.ruby-forum.com/. > > > >-- Appreciated my help? Recommend me on Working With Rails http://workingwithrails.com/person/11030-ryan-bigg --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
i could change it. right now it''s "Yes" or "No" it''s probably better coding to use the boolean maybe? it''s easier to display though with Yes or No. but i could easily just through in a helper function. Ryan Bigg wrote:> is has_manufacture a boolean field? > > On Thu, May 22, 2008 at 10:41 AM, Scott Kulik < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> >> > >> > > > -- > Appreciated my help? > Recommend me on Working With Rails > http://workingwithrails.com/person/11030-ryan-bigg-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-May-22 02:35 UTC
Re: how to set a data value before saving or destroying
On May 21, 2008, at 9:10 PM, Scott Kulik wrote:> Ryan Bigg wrote: >> What is this has_manufacture field and why is it so important? >> >> On May 21, 2008, at 8:51 PM, Scott Kulik wrote: >>> here is my code >>> >>> class Manufacture < ActiveRecord::Base >>> belongs_to :item >>> before_save :set_has_manufacture_yes >>> before_destroy :set_has_manufacture_no >>> >>> def set_has_manufacture_yes >>> self.item.has_manufacture = "Yes" >>> end >>> >>> def set_has_manufacture_no >>> self.item.has_manufacture = "No" >>> end >>> >>> For some reason the value is not getting set. Is this because I am >>> trying to set a value in the ITEMS table and not MANUFACTURES? >> > basically when someone create a manufacture i have a drop down list of > all the items that don''t already have a manufacture. so what i''m > trying > to do is an easy way to populate the drop down list with all the items > that don''t have a manufacture yet. > > here a few fields from my data structures > > ITEMS > id > name > has_manufacture > ... > > MANUFACTURES > id > item_id > ... > > so my collection select currently selects all items where > has_manufacture = "No" > > i''m not sure if there is another way i could do something like select > NOT items.manufacture.blank? > > i''m sure there is probably an easier way to do it but i''m not sure of > the logic.While I think you might mean to say "manufacturer", I think this works: class Item < ActiveRecord::Base has_one :manufacture def self.find_all_with_no_manufacture find(:all, :select => "items.*", :joins => "LEFT OUTER JOIN manufactures ON manufactures.item_id = items.id", :conditions => ''manufactures.id IS NULL'') end end You might need a :readonly=>false in there, too, if you use this for more than the drop-down. You could instead have :select => "items.id, items.name" if you just intend to create [name,id] pairs for the select list by adding .map{| i| [i.name,i.id]} (But I would name the method items_with_no_manufacture_for_select in that case so it''s clear that you aren''t getting full model objects back.) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org +1 513-295-4739 Skype: rob.biedenharn --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> On May 21, 2008, at 9:10 PM, Scott Kulik wrote: >>>> >>> >> name >> >> i''m not sure if there is another way i could do something like select >> NOT items.manufacture.blank? >> >> i''m sure there is probably an easier way to do it but i''m not sure of >> the logic. > > While I think you might mean to say "manufacturer", I think this works: > > class Item < ActiveRecord::Base > has_one :manufacture > > def self.find_all_with_no_manufacture > find(:all, :select => "items.*", > :joins => "LEFT OUTER JOIN manufactures ON > manufactures.item_id = items.id", > :conditions => ''manufactures.id IS NULL'') > end > endthanks Rob!! that worked perfectly. now i can remove the has_manufacture field!! thanks for your suggestions too ryan. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
To answer your original question... you were setting the value of the attribute, but you never called save. It''s a moot point now, but either of these should have worked: def set_has_manufacture_yes self.item.has_manufacture = "Yes" self.item.save end def set_has_manufacture_yes self.item.update_attribute(:has_manufacture, "Yes") end def set_has_manufacture_yes self.item.update_attributes(:has_manufacture=>"Yes") end On May 21, 10:52 pm, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Rob Biedenharn wrote: > > On May 21, 2008, at 9:10 PM, Scott Kulik wrote: > > >> name > > >> i''m not sure if there is another way i could do something like select > >> NOT items.manufacture.blank? > > >> i''m sure there is probably an easier way to do it but i''m not sure of > >> the logic. > > > While I think you might mean to say "manufacturer", I think this works: > > > class Item < ActiveRecord::Base > > has_one :manufacture > > > def self.find_all_with_no_manufacture > > find(:all, :select => "items.*", > > :joins => "LEFT OUTER JOIN manufactures ON > > manufactures.item_id = items.id", > > :conditions => ''manufactures.id IS NULL'') > > end > > end > > thanks Rob!! that worked perfectly. now i can remove the > has_manufacture field!! > > thanks for your suggestions too ryan. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ah ok thanks i thought the create method that was calling the save should work. thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---