Wednesday, April 11, 2012

[Rails] record a field in the database

Hi Guys,

I don't know why but I can't add a correct value in the database instead
to add the correct value I add a ID

for the schema.rb file I have

create_table "apples", :force => true do |t|
t.string "name"
t.integer "basket_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "baskets", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "tables", :force => true do |t|
t.string "basket"
t.string "apple"
t.integer "basket_id"
t.integer "apple_id"
t.datetime "created_at"
t.datetime "updated_at"
end

in the new @table form ('table' is the name of the model) I have a
dynamic select menu

<%= f.label :basket_id %>
<%= f.collection_select :basket_id, Basket.order(:name), :id, :name,
{:prompt => true} %>

<%= f.label :apple_id %>
<%= f.grouped_collection_select :apple_id, Basket.order(:name), :apple,
:name, :id, :name, {:prompt => true} %>

but when I push submit in the database I have the ID Basket and the ID
Apple e not the Basket Name and the Apple Name

I have 3 model

@apple, @basket, @table

class Apple
belongs_to :tables
belongs_to: baskets
end

class Basket
belongs_to :tables
has_many :apples
end

class Table
has_many :baskets
has_many :apples
end

and in the @table model I add to line

belongs_to :basket, :class_name => "Basket", :foreign_key => "basket_id"
belongs_to :apple, :class_name => "Apple", :foreign_key => "basket_id"

and in the confirmation page I have in the view page

<%= h @table.basket.name %>
<%= h @table.apple.name %>

and I saw correctly the name in the webpage side

for Basket I have 'small'
for Apple I have 'red'

<p>Basket: <strong> small </strong> </p>


<p>Apple: <strong> red </strong> </p>

after submit I saw in the database recorded not 'small' and 'red' but
the ID '3' and '7'

thank you in advance,

C

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate