0

My program is trying to create some groups automatically, with a prefix of 'automated_group', it won't show up when loading all groups, can't be edited, and some more stuff. But I have to limit users from doing it. But if I make a validate function, it won't let my app do it and group.save returns false. Even when updating other attributes, it won't let me save it, cause the name won't validate.

Is there any other way? Sometimes use validation, or maybe check who's changing the value?

Thanks in advance

OMY
  • 402
  • 1
  • 8
  • 18

2 Answers2

0

You can use a permission system like cancan (https://github.com/ryanb/cancan). Then you can define someting like this:

can :manage, Group, automated_group: false
  • ha, that's a good approach, The thing is I don't want to add another attribute in db, and if I did, I could've checked it in my validation function, letting it be saved if automated_group is true – OMY Jul 07 '13 at 11:45
0

I've found the half of the answer in skip certain validation method in Model

attr_accessor :automated_creation
validate :check_automated_name, unless: :automated_creation

def check_automated_name
#...
end

and in my controller:

def get_automated_group
    name = "automated_group_#{product.id}"
    group = Group.find(name: name).first
    group Group.new(automated_creation: true) unless group.blank?
    returrn group
end

When updating: I'll check in check_automated_name function that it any change on name has happened with:

group.name_changed?

so any thing else can be updated except 'name', which the only way of creation is from rails itself.

Community
  • 1
  • 1
OMY
  • 402
  • 1
  • 8
  • 18