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?

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

On Thu, May 22, 2008 at 10:21 AM, Scott K. <
[email protected]> wrote:

end


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

Ryan B. wrote:

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

On Thu, May 22, 2008 at 10:21 AM, Scott K. <
[email protected]> wrote:

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?

On Thu, May 22, 2008 at 10:41 AM, Scott K. <
[email protected]> wrote:


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

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 B. wrote:

is has_manufacture a boolean field?

On Thu, May 22, 2008 at 10:41 AM, Scott K. <
[email protected]> wrote:


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

Rob B. wrote:

On May 21, 2008, at 9:10 PM, Scott K. 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.

On May 21, 2008, at 9:10 PM, Scott K. 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

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 B. http://agileconsultingllc.com
[email protected]
+1 513-295-4739
Skype: rob.biedenharn

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 K. [email protected]

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