开发者

How do I make sure an association's attribute is correct to validate the association?

I've got a simple user: **Edited a bit for clarity, and on Sam's suggestion

class User < ActiveRecord::Base
  # <snip other attribs>
  has_many :handles
  has_one :active_handle, :class_name => "Handle"

  validates_each :active_handle, :allow_nil => true  do |record, attr, value|
    record.errors.add attr, "is not owned by the correct user" unless record.handles.include?(value)
  end
end

And a handle model:

class Handle < ActiveRecord::Base
  belongs_to :user
  attr_accessible :user_id
  validates :user_id,
        :presence => true,
        :numericality => true

  attr_accessible :name
  validates :name,
        #etc...
end

Now I'd like to check, when setting the User.active_handle association, that the handle is owned by the correct Handle.user_id. I've tried to do this in a custom validation, and also in a validate method on the User model. Both ways, it does the exact opposite of what I want, it sets the user_id of handle to the User doing the checking.

I'm at the end of my rope, clearly I don't understand something, and google isn't getting me anywhere I haven't already been.

ETA: I have also tried to manipulate the has_one association with conditions, that seems to fail too...

has_one :act开发者_开发知识库ive_handle,
        :class_name => "Handle",
        :conditions => ['user_id =?', '#{self.id}']


I'd take a different approach to this problem; the double link is messy, and hard to maintain, as you've discovered. Instead, I'd put an active flag in the handle model, and order the :active_handle association on that flag:

class User < ActiveRecord::Base
    has_many :handles
    has_one  :active_handle, :class_name => 'Handle', :order => 'handles.active DESC'
end

Now there's only one link, and it's the same link that's already in place to establish the plain has_many :handles association (namely, the user_id attribute in the Handle model). Finding the active handle in now just a matter of finding the User's first Handle with the active flag set. And I'd use :order rather that :conditions, as that'll give you a nice fallback: If no Handle for that user has the active flag set, Rails'll pick one to use as a default.

And then, in your handle model, you can have a fairly simple activate function:

class Handle < ActiveRecord::Base
    # ...stuff...

    def activate!
        # Pseudocode - sorry!
        sql('UPDATE handles SET active = 0 WHERE user_id = ?', self.user_id)
        self.active = true
        self.save!
    end
end

Or, if you wanted to be able to call something like user.handles.activate(hdl), you could put something similar as an association extension. Or user.active_handle = hdl. Or...

Hope this helps!


If your doing it from the user model then you need to add the validation to the user model instead of the handle model because the user is what's getting updated.

This is how I would validate that the user actually belongs to the handle before it can be made active.

class Handle < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :handles
  validates :handle_can_be_active?

  def handle_can_be_active?
      if self.handles.include?(self.active_handle)
          #no problem
      else
          errors.add_to_base('Must belong to the handle before it can be active!')
      end
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜