Rails implementing a remote attribute

I have a form that has many contacts. I want to be able to delete contacts without deleting them, but deleting them. I added the attribute "deleted" to the contact table.

In most cases, I need the contacts of the forms, I want only those that are not deleted:

form.contacts.find_all_by_deleted(false)

This seems messy, so I thought about named_scope or default_scope, but many people seem to say they are evil.

I also thought about just changing the attitude of the form (is it the same as the default)? This evil?):

has_many :contacts, :conditions => ["deleted = false"]

In my admin view, I need to see all the contacts. I think I can just find the find from the "Contacts" section by adding the form identifier.

It seems that this is what people will implement the party, and must have a good agreement with the best practice.

Rails 2 Rails 3. .

+3
2

1: :

class User
  has_many :active_contacts, :class_name => "Contact", 
                            :conditions => {:deleted => false}
  has_many :contacts 
end

contacts, - active_contacts.

2: default_scope:

class User
  has_many :contacts 
end

class Contact
  default_scope {:conditions => {:deleted => false}}
end

:

u.contacts

admin:

User.send(:with_exclusive_scope){u.contacts}

default_scope deleted. default_scope , .

+1

Source: https://habr.com/ru/post/1773298/


All Articles