Make Application level variable that changes on specific event - ruby-on-rails-3

In Rails, when we define Global Variable(i.e. in Application.rb file), it is Constant and we can't modifiy it. Is there any alternate available in Rails that i can use the Variable as Global variable in my Rails application and access and change it's value during execution ?

This may not be exactly what you are looking for, but you could put a function in your ApplicationController that sets a variable, then set up a before_filter :your_def. Set your variable up as an #variable and then have access/edit it any controller.

Related

Rails update_attribute replacement?

Is there a replacement for update_attribute? I know you can still use it in Rails 3 but you get deprecation messages.
The reason why I need to use update_attribute is because I need to skip validations but run callbacks.
The only way that I've found of doing this (and avoid deprecation messages), is by simply extracting the code from update_attribute:
Object.send("#{attribute}=", value)
Object.save(:validate => false)
I'd like to know if there is another (proper) way of doing this.
Thanks.
I would suggest what the answer is no.
You cannot pass false to update_attributes to skip validation.
I would recommend you update_column, but this method skip callbacks. And judging by you question it isn't what you need.
Update:
I've found interesting table in that presentation. That I guess can prove my opinion.
Judging by this table there is only one method which satisfies the conditions.
Would you consider using conditional validation? http://guides.rubyonrails.org/active_record_validations_callbacks.html#conditional-validation
You could set an attribute on the object to disable the validation in question for the current record.
From the validations documentation:
The validation process on save can be skipped by passing :validate => false. The regular #save method is replaced with this when the validations module is mixed in, which it is by default.
So simply set your attribute to the correct value and call save with the validates parameter set to false. You do not need to use send, either, unless you have code that relies on metaprogramming, but that is not obvious from your example.
object.attribute = value
object.save(:validate => false)

How to initiate global variable in Squeak

I don't mean a class variable. I want a variable that can be used everywhere. How should I initiate it?
I know one way is Smalltalk at: #variableName put: theValue. but I don't want to give it value as soon as I create it.
This should do the trick:
Smalltalk at: #VariableName put: nil
When you create a global variable in the Smalltalk dictionary, you are creating an Association which has a key and a value, so you can't get away without providing some sort of value.
You can come back later and change the value with:
Smalltalk at: #VariableName put: newValue
Any compiled code that references the global variable will see the new value, because the compiled code references the Association.

Rails 3 - Need shared model methods for polymorphic models

Currently I have repeated code in multiple modules something like this:
def do_something_polymorphic
self.something_polymorphic_able.where(.....).each do |thing|
...
end
end
In the spirit of DRY, I tried moving do_something_polymorphic() into a module at /lib/shared_methods.rb. When I added include SharedMethods in my models I got an error:
uninitialized constant Chapter::SharedMethods (NameError)
Which I could not figure out how to get around.
Then I tried loading the module file into the models (load 'shared_methods.rb'). It loaded OK, but the Module construct threw off the namespace and do_something_polymorphic() was undefined for the model in which it was loaded/included. So "Module" appears not to be an option if you are trying to self-reference an object.
Lastly, I removed the module construct from the shared_methods.rb file and just left the do_something_polymorphic() method alone in the file. It loaded OK, but when I ran the method I got the error:
NoMethodError: private method `do_something_polymorphic' called for #<Polymorphic_Object:0x007fc27e5b8338>
Not sure where to go from here. I could go back to setting this up as a module and try to pass "self" in as an object parameter to the method, but I would like to preserve the ability to cleanly call the method against its object: current_object.do_something_polymorphic
Short of going the inheritance route (which I really want to avoid,) is there a way to share, across multiple models, a method that uses the self keyword?
You can include lib/ modules automatically by modifying the application configuration.
#config/application.rb
config.autoload_paths += %W(#{config.root}/lib)

How can a variable be part of another variables name in lua?

I want to set dynamic variable names.
such as
function make(name)
local name..bar = "ipsum"
end
make(foo)
this possible?
For globals it's simply indexing like _G[name..bar]. For locals you could emulate this by setting all globals you use in a local table, and index that one. For an approach to really use a local, I can't help you.

Ruby on rails: model name uninitialized error (mismatch of module)

I am accessing model Company::Item in controller Security::MyController. It is giving error uninitialised constant Security::Company::Item . So basically it is appending 'Security::' for given model. It is not the case with some other models say Security::User(model in same module security). What could be possible explanation for this?
This is a scope resolution problem. You should try using ::Company::Iteminside Security::MyController
According to Ruby Language Spec
::Something is a shortcut for Object::Something. The idea is that ::Something
should look up a constant called Something in the global scope, but since ruby
doesn't truly have a global scope, it looks it up in the Object class, which is
the nearest thing in ruby to a global scope.
Prefixing :: will prevent Ruby from applying the default scope in that context, which in your case, is the Security:: scope