Skip to content

Instantly share code, notes, and snippets.

@universal
Created March 23, 2015 11:29
Show Gist options
  • Select an option

  • Save universal/02429a99a27d4780d505 to your computer and use it in GitHub Desktop.

Select an option

Save universal/02429a99a27d4780d505 to your computer and use it in GitHub Desktop.
Schemas
An update_call of the item_classification
Processing ItemClassificationsController#update (for 127.0.0.1 at 2015-03-23 11:15:54) [PUT]
Parameters: {"_method"=>"put", "commit"=>"Update", "action"=>"update", "controller"=>"item_classifications", "id"=>"4", "item_classification"=>{"title_translation"=>{"value"=>"This is a title", "locale_id"=>"2"}}}
Example of the problem:
>> item_classification = ItemClassification.find(4)
=> #<ItemClassification id: 4, previous_level_id: 0, level: 1, name: "Objects", permalink: "objects", position: 5, items_count: 4802, description: "a description", enabled: true,...">
>> item_classification.build_title_translation(:value => 'This is a title', :locale_id => 2)
#<TitleTranslation id: nil, locale_id: 2, key: "metatags.titles.item_classification_4.title", value: "This is a title", updated_at: nil>
>> item_classification.save
=> true
>> item_classification.title_translation
=> #<TitleTranslation id: 10329, locale_id: 2, key: 4, value: "Prodotti per bambini", updated_at: "2015-03-23 10:25:23">
What am I doing wrong?
class ItemClassification < ActiveRecord::Base
has_one :title_translation, :foreign_key => 'key', :primary_key => :custom_title_key, :conditions => ['locale_id = 2']
accepts_nested_attributes_for :title_translation
private
def custom_title_key
"metatags.titles.item_classification_#{id}.title"
end
end
create_table "TitleTranslation", :force => true do |t|
t.integer "locale_id"
t.string "key"
t.text "value"
t.datetime "updated_at"
end
create_table "item_classifications", :force => true do |t|
t.integer "previous_level_id"
t.integer "level"
t.string "name"
t.string "permalink"
t.integer "position"
t.integer "items_count", :default => 0
t.text "description"
t.boolean "enabled"
end
class TitleTranslation < ActiveRecord::Base
belongs_to :item_classification, :foreign_key => 'key', :primary_key => :custom_title_key
attr_accessible :item_classification
def custom_title_key
"metatags.titles.item_classification_#{item_classification.id}.title"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment