when we write simple find method of any tables it returns
[#, #, #, #] in this way
Sounds like your view just contains <%= some_array %>
Which means that you'll just get the result of calling to_s on each of
your array elements, which is unlikely to be useful and also invalid
html since what it outputs is something like #<SomeObject:
0x12345678> . It is up to you to produce some meaningful textual
output for your array elements (eg <%= h some_array.inspect %> but
even that is pretty crude)
when we use find method and print that object it shows like that
[#, #, #, #]
That's probably because of exactly what I said. the default to_s/
inspect produces output like #<ClassName: ...> but that's not legal
html so it displays as just #. If you want something useful to be
displayed that's up to you, eg <%= records.collect {|r| r.name}.join
(', ') %> (assuming what you wanted to display was the name attribute
of the records. If you want to display all the attributes you could
probably just call inspect on the attributes property of each of your
records.
but after modifying this array with
[{zip=>'d',{po => 'd'}}]
then we can't use that directly in view it should some thing i have to
write in file file liek
Well you've changed your array of active record objects into a hash or
an array of hashes - you just need to access that appropriately
(exactly how will depend on what you did to your original array).
I think there is some confusion in this conversation
first of all: When you do that
@users = User.find(:all)
@users will be one array of users objects, and if user object answer
to the method name you can do:
@users.each do |u| puts u.name end
and you will get all users names,
But if you have somthing like this:
@users = [{:name => "john"}, {:name => "miguel"}]
you cannot do :
@users.each do |u| puts u.name end
because {:name => "jonh"} is an hash and it do not respond to method
name, although you can get the name if you do this {:name => "jonh"}
[:name]
So summarizing it:
a = User.new :name => "foo"
a.name # WORKS
a = {:name => "foo"}
a.name # DO NOT WORK
a[:name] # WORK
depends what kind of object you want to create. You can't call
user.foo unless the user object has a foo method, and hashes don't. So
create an instance of something that does have a foo method. Exactly
what you create and how you use your hash when creating it is entirely
up to you.