मुझे इंटरनेट पर कोई कोड नहीं मिला जो काम करता है, इसलिए मैंने कुछ शोध किया और अपना खुद का लिखा। यह रेल 3.2.x और शायद कुछ अन्य संस्करणों के लिए काम करता है।
सम्मिलित यह आपके ApplicationController
# Set a filter that is invoked on every request
before_filter :_set_current_session
protected
def _set_current_session
# Define an accessor. The session is always in the current controller
# instance in @_request.session. So we need a way to access this in
# our model
accessor = instance_variable_get(:@_request)
# This defines a method session in ActiveRecord::Base. If your model
# inherits from another Base Class (when using MongoMapper or similar),
# insert the class here.
ActiveRecord::Base.send(:define_method, "session", proc {accessor.session})
end
में मैं आपको याद दिलाना नहीं होगा कि एक मॉडल से अपने सत्र तक पहुँचने बुरा कोड को जन्म दे सकती। Other posts may tell you, that you are stupid। यद्यपि आपके मॉडल से सत्र तक पहुंचने के कुछ वैध कारण हैं, जैसे Model.save
विधि को लागू करना, जो वर्तमान उपयोगकर्ता सत्र में सहेजता है।
धन्यवाद निमेश। यहां वह परिदृश्य है जहां मुझे मॉडल में सत्र तक पहुंचने की आवश्यकता है: 1. प्रत्येक रिकॉर्ड के लिए - सत्र से मूल्य के साथ - एक क्षेत्र को अद्यतन करने के लिए ActiveRecord पर्यवेक्षक में। 2. current_user (restful_authentication) का एक फ़ील्ड सेट करना। क्या प्रत्येक उपयोगकर्ता अलग-अलग मूल्य रखता है जो लॉगिन पर सेट किया जा रहा है? – Saim
हां। बस यहां फिर से देखें http: //www.zorched.net/2007/05/29/making- सत्र- डेटा- उपलब्ध-to-models-in-ruby-on-rails/... इसमें 'first_filter: set_user है जो थ्रेड के लिए प्रत्येक नए अनुरोध पर सत्र मूल्य बचाता है। इसका मतलब है कि प्रत्येक उपयोगकर्ता के पास अलग-अलग मूल्य होता है क्योंकि हर कोई अनुरोध अलग होता है और इस तरह से थ्रेड अलग-अलग मानों के साथ अद्यतन करने पर रहता है –