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:
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])
def getFrontendLanguage()
if @session[:language].nil? || @session[:language].blank? @session[:language] = getFrontendDefaultLanguage()
end
return @session[:language]
end
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
@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!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.