how to set a data value before saving or destroying

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?

Ryan Bigg wrote:

What is this has_manufacture field and why is it so important?

   end
>

--
Appreciated my help?
Recommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

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.

oops i forgot to mention my models

ITEMS
has_one :manufacture

MANUFACTURES
belongs_to :item

is has_manufacture a boolean field?

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:

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@AgileConsultingLLC.com
+1 513-295-4739
Skype: rob.biedenharn

Rob Biedenharn wrote:

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

ah ok thanks i thought the create method that was calling the save
should work. thanks!