0

Using Rails 3.2.9
I'm attempting to build a association using .build instead of .create but getting a password validation error that I can't seem to find a workaround.
Review below:

The way I understand to save a item with a association that is built using .build you actually have to do the save in this case on the owner. If you do a save on the @item it just creates the item and not the association(meaning it doesn't save to the DB until current_owner.save). when i do a save on the owner i hit a error due to password not meeting the validation requirements. Is there a way to bypass the validation when I do a save, due i need to implement a different solution for password, or just stop complaining and use .create instead of .build.

The below gives a password does not meet validation error

@item = current_owner.items.build(params[:item])
   if current_owner.save
       Do some other work with item
   end

I guess i could do the following(for some reason it seems dirty to me maybe not. Thoughts?)

 @item = current_owner.items.create(params[:item])
 if !@item.nil?
       Do some other work with item
 end

Table Setup: owners:

  • id
  • name
  • encrypted_password
  • salt

items:

  • id
  • name

items_owners:

  • owner_id
  • item_id

Models:

class Item < ActiveRecord::Base
   attr_accessible :description, :name, :owner_ids

   has_many :items_owner
   has_many :owners, :through => :items_owner


end

class Owner < ActiveRecord::Base
   attr_accessor :password
   attr_accessible :name, :password, password_confirmation

   has_many :items_owner
   has_many :items, :through => :items_owner
   before_save :encrypt_password

   validates :password, :presence => true,
        :confirmation => true,
        :length => { :within => 6..40 }
end

class ItemsOwner < ActiveRecord::Base
   attr_accessible :owner_id, :item_id

   belongs_to :item
   belongs_to :owner
end
Sinble
  • 310
  • 1
  • 2
  • 9

2 Answers2

0

I didn't understand very much your question. Hope this helps:

@item = current_owner.items.build(params[:item])
if @item.valid?
  # item is valid and ready to save to DB
else
  # item is not valid.
end
macool
  • 659
  • 6
  • 11
  • updated explanation. and reason i'm doing a current_owner.save instead of item.save – Sinble Nov 27 '12 at 16:33
  • If you save the item itself, it DOES set the association too. – macool Nov 27 '12 at 16:42
  • It does in memory for that object.so if i did a current_owner.items it would show up in the list. However, if i do a Owner.find(1).items(assuming current_owner id is 1) it would not show up because i didn't execute the save on current_owner. Just tested it to make sure i wasn't crazy. – Sinble Nov 27 '12 at 17:06
  • Of course it wont be showing, because it's not saved to DB. You newbie? – macool Nov 27 '12 at 17:56
  • I guess I didn't understand ur answer. U stated if u did a item.save it would create the item and association to owner I'm stating it doesn't save it to the DB until u do a owner.save. I need it to save to the DB. – Sinble Nov 27 '12 at 18:05
  • since password is a accessor and not a actual parameter in the db. when i try to save the recored i get a password cannot be nil. yes i'm a newbie ;) – Sinble Nov 27 '12 at 19:01
  • You can save without running validations if you want to avoid raising that error: `owner.save(:validate => false)` – macool Nov 27 '12 at 23:05
0

You have password validations in your model which requires the presence of password. What you can do is as follow

@item = current_owner.items.build(params[:item])
if @item.valid?
   @item.save
  # do stuff what you want
else
  # item is not valid.
end

Hope it would help

Muhamamd Awais
  • 2,385
  • 14
  • 25
  • updated explanation. and reason i'm doing a current_owner.save instead of item.save – Sinble Nov 27 '12 at 16:33
  • if you do; user.credit_cards = [ ] and then user.save it would set the user credit cards to nil, plus when you build the item rails will automatically build all the mentioned association in the model – Muhamamd Awais Nov 27 '12 at 17:11
  • I'm not understanding your credit_card reference to my password validation problem. Yes i understand when you use build it will automatically build the associations. the problem is when i go to save Owner.save(which will save the associations) i'm getting the password cannot be null. – Sinble Nov 27 '12 at 18:57