No I would loop them as each needs to be a seperate record. However I just can’t figure out how to call them.
{“cantitle”=>{“1”=>{“title_opt”=>“CEO”}, “2”=>{“title_opt”=>“COO”},> “3”=>{“title_opt”=>“CIO”}},
The formfields are like this <%text_field :cantitle, :title_opt, index => 1%>
<%text_field :cantitle, :title_opt, index => 2%>
<%text_field :cantitle, :title_opt, index => 3%>
I had to add index to make them all individual params.
Just can’t figure out what they are called. ??
"[cantitle][:1][:title_opt] etc ? Maybe, not in front of my dev machine right now.
Not 100% sure what you really need, bu i think you mixed it up,
as you try to manipulate a "cantitle" object before you have one,
and you should use build, not create.
for each params set in params[:cantitle]:
1) build a cantitle object with the parameters (v)
2) add the @candidate_id
3) add the current_user.id
4) save the cantitle object
params[:cantitle].each do |k, v|
cantitle = Cantitle.build(v)
cantitle.candidate_id = @candidate_id
cantitle.user_id = current_user.id
cantitle.save
end
I assume @candidate_id is initiated and filled before that pice of
code...
Question for Ezra: i relied on your example for this, but im a bit
confused:
when i do :
params[:cantitle].each do |k, v|
is k = 'title_opt' and v = k's value ?
and wouldnt create() or build() require the key => value pair instead
of just v (the value) ?
or am i mixing something up here ?
I still have to learn much about ruby, i know more about the logic in
rails than about the
actual language and syntax beneath
k,v| means:
k is the key of the array (meaning: the number of the arrays entry)
v is the hash in each array entry { title_opt => someValue }
Okay, at first I thought I had a multidimensional array. Which I guess it still is ?
But each array is a hash ?
I’m not looking for any explanation, I’m sure it will eventually sink in. I think I’m more used to seeing the v stand for value.
well the hash <-> array stuff confused and still confuses me from time
to time...
if you ever had experience with another scripting language, this
analogy might help:
arrays with number keys are also arrays in ruby.
arrays with named keys are known hashes in ruby, and they have no
specific order, like numbered arrays have.
params[:cantile] contains an array (or a hash if the numbers are given
as strings)...
{"1"=>somevalue1, "2"=>somevalue2, "3"=>somevalue3},
so params[:cantitle].each do |k,v| gives you each object in that
arrays/hash
with k = the number and v = somevalue
but each v contains a hash: {:title_opt => value } (a named key and a
correspnind value are a hash)
dont know if i used the right vocabulary in every place but i think
you might get the idea ...
well the hash ↔ array stuff confused and still confuses me from time
to time…
if you ever had experience with another scripting language, this
analogy might help:
arrays with number keys are also arrays in ruby.
arrays with named keys are known hashes in ruby, and they have no
specific order, like numbered arrays have.
I think arrays with named keys are hashes in other languages as well. If I remember, PHP was the same.
params[:cantile] contains an array (or a hash if the numbers are given
as strings)…
so params[:cantitle].each do |k,v| gives you each object in that
arrays/hash
with k = the number and v = somevalue
but each v contains a hash: {:title_opt => value } (a named key and a
correspnind value are a hash)
dont know if i used the right vocabulary in every place but i think
you might get the idea …
Yes, I do get the idea.
So now
As my next challenge.
I have this situation - where I’m collecting a city and state,
I believe (haven’t gotten my hands dirty yet)
where the params are coming through as such
Actually I was pretty surprised at the nice neat way they get send. I mean since they are both part of the same model, Rails seems to join them together nicely in the array or hashes
Current thinking is state_id gets nested inside of the city loop ?
well the hash <-> array stuff confused and still confuses me
from time to time...
if you ever had experience with another scripting language, this
analogy might help:
arrays with number keys are also arrays in ruby.
arrays with named keys are known hashes in ruby, and they
have no specific order, like numbered arrays have.
If by named keys, you mean strings or symbols then your definition
is not correct.
From the doc :
A +Hash+ is a collection of key-value pairs. It is similar to an
+Array+, except that indexing is done via arbitrary keys of any
object type, not an integer index. The order in which you traverse
a hash by either key or value may seem arbitrary, and will
generally not be in the insertion order.
You've got key-value pairs. But for key you can use any object,
and the same for value.
As you can see, I've used an array as a key so b[a] works.
With Rails, we manipulate often Hash-like objects called
HashWithIndifferentAccess objects. The difference is that
string key and symbol key give access to the same value, e.g.
options['foo'] is the same as options[:foo]
Rails also has an OrderedHash class, that is, key-value pairs
are ordered.
So what I have here is an array or an array of arrays (below) ? Or did I thoroughly miss your point?
.
“canlocation”=>{“1”=>{“city”=>
“Phoenix”,
“state_id”=>“3”},
“2”=>{“city”=>“Boston”,
“state_id”=>“19”},
“3”=>{“city”=>“Seattle”,
“state_id”=>“47”}}}
Also, I notice some odd behaviour in that if no values are created, this array of arrays still exists.
Example:
If I have a field set up like such:
Which is still multiple values but in one array, if nothing is selected there are no params.
Worked it out -
a = params[‘cantitle’]
a.each {|k, v|
cantitle = Cantitle.new
cantitle.candidate_id = @candidate_id
cantitle.user_id = current_user.id
unless v[:title_opt].blank? # checks here for no value.
cantitle.title_opt = v[:title_opt]
cantitle.save
end
}