Accessing the session in a model

Hey
i want to acces the session in the model, i have 3 columns, name_nl,
name_en, name_fr, depending on what is in @session[:language], i need to
select to correct field.

i have something like this for a product item
id int(11) Nee auto_increment
name_en varchar(100) utf8_general_ci Nee
name_fr varchar(100) utf8_general_ci Nee
name_nl varchar(100) utf8_general_ci Nee
price decimal(10,2) Nee 0.00
publish tinyint(1) Nee 0
publish_from date Ja 0000-00-00
publish_to date Ja 0000-00-00
created_at datetime Nee 0000-00-00 00:00:00
created_by varchar(250) utf8_general_ci Ja NULL
updated_at datetime Nee 0000-00-00 00:00:00
updated_by varchar(250) utf8_general_ci Ja NULL
deleted_at datetime Ja 0000-00-00 00:00:00
Selecteer alles / Deselecteer alles Met geselecteerd:

Sorry, pc got stuck here is the full message

Hey
i want to acces the session in the model, i have 3 columns, name_nl,
name_en, name_fr, depending on what is in @session[:language], i need to
select to correct field.

i have something like this for a product item
id int(11) Nee auto_increment
name_en varchar(100) utf8_general_ci Nee
name_fr varchar(100) utf8_general_ci Nee
name_nl varchar(100) utf8_general_ci Nee
price decimal(10,2) Nee 0.00
publish tinyint(1) Nee 0
publish_from date Ja 0000-00-00
publish_to date Ja 0000-00-00
created_at datetime Nee 0000-00-00 00:00:00
created_by varchar(250) utf8_general_ci Ja NULL
updated_at datetime Nee 0000-00-00 00:00:00
updated_by varchar(250) utf8_general_ci Ja NULL
deleted_at datetime Ja 0000-00-00 00:00:00

this is my code to fetch the name field

class Product < ActiveRecord::Base
def name
case @session[:language]
when “nl”
return self.name_nl
when “en”
return self.name_en
when “fr”
return self.name_fr
end
end
end


i can always use something like this as datastructure
product:

id int(11) Nee auto_increment
price decimal(10,2) Nee 0.00
publish tinyint(1) Nee 0
publish_from date Ja 0000-00-00
publish_to date Ja 0000-00-00
created_at datetime Nee 0000-00-00 00:00:00
created_by varchar(250) utf8_general_ci Ja NULL
updated_at datetime Nee 0000-00-00 00:00:00
updated_by varchar(250) utf8_general_ci Ja NULL
deleted_at datetime Ja 0000-00-00 00:00:00
productlang:

id int(11) Nee auto_increment
name varchar(100) utf8_general_ci Nee
lang varchar(4) utf8_general_ci Nee
parent int(11) Nee 0

saving the language depended fields in another table, but then i need to
use encapsulation. and i have tried that before, but then there was an
update of rails and it didnt work anymore. So i want to use something
good instead.

Anyone has an good idea? Or can help me with this?

I need also validation on the virtual field “name”. Not on name_fr,
name_nl, name_en cuz these can be empty.
Like name => length max 100 chars => then the value will be copied
(before saving) to the correct field (depending on @session[:language])

Thx

Or in stead of:

case @session[:language]

i want

case getFrontendLanguage()

This in the application controller:

def getFrontendLanguage()
if @session[:language].nil? || @session[:language].blank?
@session[:language] = getFrontendDefaultLanguage()
end
return @session[:language]
end

def getFrontendDefaultLanguage()
doc = REXML::Document.new File.new(“config/project.xml”)
doc.elements.each("*/languages/frontend") { |element|
return element.attributes[“default_lang”]
}
end

project.xml:

thx

Hi Nick,

Nick B. wrote:

case @session[:language]

First thing to do is stop using ‘@session[]’. It’s use has been
depracated.
Use ‘session[]’ instead. You’ll have problems otherwise.

hth,
Bill

Not sure exactly what’s the best way to do it, but I would think about
creating a virtual field for your model that contains the language value
you need. Pass the session variable into your model’s virtual field
when creating or updating an object.

Avoid session use in the model. It’s ill advised and goes against MVC.
You
may someday want to use your models in a CRON job or an external process
that does not require the web. Sessions are a controller piece.

Try this instead:

class Product < ActiveRecord::Base
attr_accessor :language
def name
case self.language
when “nl”
return self.name_nl
when “en”
return self.name_en
when “fr”
return self.name_fr
end
end
end

@product = Product.find 1
@product.language = session[:language]
@product.name

if @product.language = session[:language] gets to be too cumbersome to
type, wrap it in a method you call

In application.rb add this:

finds the product and sets the language type based on session contents

product id defaults to params[:id] if no product id is provided.

def get_product(id = params[:id])
product = Product.find 1
product.language = session[:language]
product
end

Now you just do

def show
@product = get_product
end

This gets more complicated in collections though

@products = Product.find :all
@products.each do |product|
product.language = session[:language]
end

But you could wrap that too.

Hope that helps you out. Looks like you’re doing internationalization.
You
might look into some plugins for that. I’ve not dealt with that yet but
I
know others are. Why not ask the list for some advice on the problem
you’re
trying to solve as well… I know others would be interested in the
response.

Have a nice day!