Has_many relation handling

Hello,

please have a look at this:

My tables:

create_table :languages do |t|
  t.column :name, :string, :limit => 3
  t.column :title, :string, :limit => 30
end

create_table :categories do |t|
  t.column :category_id, :integer (3.)
  t.column :created_at, :timestamp
  t.column :updated_at, :timestamp
end

create_table :categorytranslations, :id => false do |t|
  t.column :category_id, :integer
  t.column :language_id, :integer
  t.column :name, :string, :limit => 60
  t.column :description, :string, :limit => 120
end

My models:

class Category < ActiveRecord::Base
has_one :category (3.)
has_many :categorytranslations, :dependent => true (1.)
has_many :languages, :through => :categorytranslations (2.)
end

class Categorytranslation < ActiveRecord::Base
belongs_to :category (1.)
belongs_to :language (1.)

validates_length_of :name, :within => 2…60
validates_length_of :description, :maximum => 120
validates_uniqueness_of :language_id, :scope => :category_id
end

Do the relations what I think they should do?..

  1. A category should have many categorytranslations and each translation
    has a corresponding language.
  2. A category “knows” all languages used in its translations.
  3. A category has one related category (parent category in tree).

If it is ok…
How do I add a new Categorytranslation to Category? So how do I access
it from Category?

At the moment I’ve only found this manual stuff:

c = Category.new :category_id => 0
c.save
t = c.categorytranslations.new :category_id => c.id, :language_id => 0,
name => ‘Cars’, :description => ‘Cars, trucks’
t.save

Can this for example category_id => c.id be handled by Rails?

Thanks
Markus

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs