How to make a Rails model read-only except for specified attributes?











up vote
0
down vote

favorite












I have a rails active record model Application and need to make it read-only.



I know that having a before_validation method with:



if locked

def readonly?
true
end

end


Will allow me to make it readonly.



The only issue is that, when I need to disable readonly mode, I can not change the locked value to false because the model is already read-only.



I know that attr_readonly :name, :email, :price etc. will allow me to make specific attributes readonly, I am looking for the inverse of this to only allow specific attributes.



Is there a way to make this read-only method except specific attributes?










share|improve this question






















  • You can store settings into a DB table as json string or use some gem like github.com/huacnlee/rails-settings-cached
    – iGian
    Nov 22 at 20:37










  • @iGian - Is there any other way that doesn't require adding a gem? I was hoping there is a built in method. Otherwise, is there a way to set read only status on a model based on one value within the model?
    – notADevAccount
    Nov 26 at 11:19










  • You can define a model Settings, where each column stores a specific parameter. Or store the information in rows as a json string. Then, the validation of an action requires to access the Settings and check which operation is allowed. Also you could access Settings from the controller, to lock edit and new or whathever.
    – iGian
    Nov 26 at 11:56










  • @iGian - The only issue I have with that is that: If my model is 'Applications', then I need to lock each of the applications within depending of if the read only attr is true. A setting model will work, if I wanted to make all applications read only, but not if I need to make individual records read only.
    – notADevAccount
    Nov 26 at 12:54










  • Add a flag to each record (new column), accessible only to users with privileges.
    – iGian
    Nov 26 at 12:57















up vote
0
down vote

favorite












I have a rails active record model Application and need to make it read-only.



I know that having a before_validation method with:



if locked

def readonly?
true
end

end


Will allow me to make it readonly.



The only issue is that, when I need to disable readonly mode, I can not change the locked value to false because the model is already read-only.



I know that attr_readonly :name, :email, :price etc. will allow me to make specific attributes readonly, I am looking for the inverse of this to only allow specific attributes.



Is there a way to make this read-only method except specific attributes?










share|improve this question






















  • You can store settings into a DB table as json string or use some gem like github.com/huacnlee/rails-settings-cached
    – iGian
    Nov 22 at 20:37










  • @iGian - Is there any other way that doesn't require adding a gem? I was hoping there is a built in method. Otherwise, is there a way to set read only status on a model based on one value within the model?
    – notADevAccount
    Nov 26 at 11:19










  • You can define a model Settings, where each column stores a specific parameter. Or store the information in rows as a json string. Then, the validation of an action requires to access the Settings and check which operation is allowed. Also you could access Settings from the controller, to lock edit and new or whathever.
    – iGian
    Nov 26 at 11:56










  • @iGian - The only issue I have with that is that: If my model is 'Applications', then I need to lock each of the applications within depending of if the read only attr is true. A setting model will work, if I wanted to make all applications read only, but not if I need to make individual records read only.
    – notADevAccount
    Nov 26 at 12:54










  • Add a flag to each record (new column), accessible only to users with privileges.
    – iGian
    Nov 26 at 12:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a rails active record model Application and need to make it read-only.



I know that having a before_validation method with:



if locked

def readonly?
true
end

end


Will allow me to make it readonly.



The only issue is that, when I need to disable readonly mode, I can not change the locked value to false because the model is already read-only.



I know that attr_readonly :name, :email, :price etc. will allow me to make specific attributes readonly, I am looking for the inverse of this to only allow specific attributes.



Is there a way to make this read-only method except specific attributes?










share|improve this question













I have a rails active record model Application and need to make it read-only.



I know that having a before_validation method with:



if locked

def readonly?
true
end

end


Will allow me to make it readonly.



The only issue is that, when I need to disable readonly mode, I can not change the locked value to false because the model is already read-only.



I know that attr_readonly :name, :email, :price etc. will allow me to make specific attributes readonly, I am looking for the inverse of this to only allow specific attributes.



Is there a way to make this read-only method except specific attributes?







ruby-on-rails activerecord methods






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 13:45









notADevAccount

84




84












  • You can store settings into a DB table as json string or use some gem like github.com/huacnlee/rails-settings-cached
    – iGian
    Nov 22 at 20:37










  • @iGian - Is there any other way that doesn't require adding a gem? I was hoping there is a built in method. Otherwise, is there a way to set read only status on a model based on one value within the model?
    – notADevAccount
    Nov 26 at 11:19










  • You can define a model Settings, where each column stores a specific parameter. Or store the information in rows as a json string. Then, the validation of an action requires to access the Settings and check which operation is allowed. Also you could access Settings from the controller, to lock edit and new or whathever.
    – iGian
    Nov 26 at 11:56










  • @iGian - The only issue I have with that is that: If my model is 'Applications', then I need to lock each of the applications within depending of if the read only attr is true. A setting model will work, if I wanted to make all applications read only, but not if I need to make individual records read only.
    – notADevAccount
    Nov 26 at 12:54










  • Add a flag to each record (new column), accessible only to users with privileges.
    – iGian
    Nov 26 at 12:57


















  • You can store settings into a DB table as json string or use some gem like github.com/huacnlee/rails-settings-cached
    – iGian
    Nov 22 at 20:37










  • @iGian - Is there any other way that doesn't require adding a gem? I was hoping there is a built in method. Otherwise, is there a way to set read only status on a model based on one value within the model?
    – notADevAccount
    Nov 26 at 11:19










  • You can define a model Settings, where each column stores a specific parameter. Or store the information in rows as a json string. Then, the validation of an action requires to access the Settings and check which operation is allowed. Also you could access Settings from the controller, to lock edit and new or whathever.
    – iGian
    Nov 26 at 11:56










  • @iGian - The only issue I have with that is that: If my model is 'Applications', then I need to lock each of the applications within depending of if the read only attr is true. A setting model will work, if I wanted to make all applications read only, but not if I need to make individual records read only.
    – notADevAccount
    Nov 26 at 12:54










  • Add a flag to each record (new column), accessible only to users with privileges.
    – iGian
    Nov 26 at 12:57
















You can store settings into a DB table as json string or use some gem like github.com/huacnlee/rails-settings-cached
– iGian
Nov 22 at 20:37




You can store settings into a DB table as json string or use some gem like github.com/huacnlee/rails-settings-cached
– iGian
Nov 22 at 20:37












@iGian - Is there any other way that doesn't require adding a gem? I was hoping there is a built in method. Otherwise, is there a way to set read only status on a model based on one value within the model?
– notADevAccount
Nov 26 at 11:19




@iGian - Is there any other way that doesn't require adding a gem? I was hoping there is a built in method. Otherwise, is there a way to set read only status on a model based on one value within the model?
– notADevAccount
Nov 26 at 11:19












You can define a model Settings, where each column stores a specific parameter. Or store the information in rows as a json string. Then, the validation of an action requires to access the Settings and check which operation is allowed. Also you could access Settings from the controller, to lock edit and new or whathever.
– iGian
Nov 26 at 11:56




You can define a model Settings, where each column stores a specific parameter. Or store the information in rows as a json string. Then, the validation of an action requires to access the Settings and check which operation is allowed. Also you could access Settings from the controller, to lock edit and new or whathever.
– iGian
Nov 26 at 11:56












@iGian - The only issue I have with that is that: If my model is 'Applications', then I need to lock each of the applications within depending of if the read only attr is true. A setting model will work, if I wanted to make all applications read only, but not if I need to make individual records read only.
– notADevAccount
Nov 26 at 12:54




@iGian - The only issue I have with that is that: If my model is 'Applications', then I need to lock each of the applications within depending of if the read only attr is true. A setting model will work, if I wanted to make all applications read only, but not if I need to make individual records read only.
– notADevAccount
Nov 26 at 12:54












Add a flag to each record (new column), accessible only to users with privileges.
– iGian
Nov 26 at 12:57




Add a flag to each record (new column), accessible only to users with privileges.
– iGian
Nov 26 at 12:57

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432372%2fhow-to-make-a-rails-model-read-only-except-for-specified-attributes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432372%2fhow-to-make-a-rails-model-read-only-except-for-specified-attributes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)