开发者

In Rails 3 how can you access the user_id of the last user in the database in your model?

Edit See Below:

class Post < ActiveRecord::Base
  last_user_id = User.last.id
  validates_inclusion_of :user_id, :in => 0..last_user_id
end

The above solution works but as vojlo explains, once in production the code will only be executed once and the model will then validate against an incorrect range of users.

I'm working on a tutorial (rails 3.0.3) and have tried for the last half hour to figure out how to tell rails that one of the classes in my model should make sure the :user_id is within the range zero to the user_id of the last user in the database.

I know I need to be using:

validates_inclusion_of :user_id, :in 0..(can't figure out this piece)

I was able to easily ensure the number entered for User_ID in numeric with:

开发者_JAVA技巧
validates_numericality_of :user_id

I'm looking for information on where to research this, I took a good look at the ActiveRecord Validators documentation and didn't find much there.


What happens if a User deletes their account? Don't you really just want to check for existence of the user?

class Post < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :user
  ...

Otherwise it's this pattern, which evaluates the post for validity against your requirements - the user_id is greater than zero and less than the largest user ID currently in the database:

class Post < ActiveRecord::Base
  include ActiveModel::Validations
  validates_with UserIdValidator
  ...

class UserIdValidator < ActiveModel::Validator
  def validate(record)
    max_user = User.find(:one, :order=>["id"])
    unless(user_id > max_user.id && user_id > 0)
      record.errors[:base] << "This record is invalid"
    end
  end
end

But I still don't quite understand why you would want to do this - is there something particularly special about your user id? I'd recommend the first approach.


Range from zero to last user id? The only way is writing a new validator.

I don't understand your purpose, but check out validates_associated.


Since you're just doing this for learnings sake:

(A) Check for "last id" presuming it is the "largest id value":

class Post < ActiveRecord::Base
  last_user_id = ActiveRecord::Base.connection.select_one('SELECT MAX(ID) AS "MAX_ID" FROM users')["MAX_ID"]
  validates_inclusion_of :user_id, :in => 0..last_user_id
end

or

(B) MAX() will get the max value of the ID field. This may not necessarily be the "latest" record inserted. If you really want to get the "last user inserted" you could do this (checks for the latest time of insertion of the record):

class Post < ActiveRecord::Base
  last_user_id = ActiveRecord::Base.connection.select_one('SELECT ID AS "LAST_ID" FROM users WHERE created_at = (SELECT MAX(created_at) from users LIMIT1)')["LAST_ID"]
  validates_inclusion_of :user_id, :in => 0..last_user_id
end


Why not use validates_uniquness_of :user_id?

Assuming that the range of 0..the_latest_user_id contains all existing user ids, then you are simply looking to see if it is unique. Determining its uniqueness would be the same thing as determining if it is in that range.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜