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
I hope that helps you
On Feb 21, 12:06 pm, Cyrus Dev <[email protected]>
wrote:
> No fred
>
> its not something like that this 2 types of array are total different it
> seems like that
>
> becuase if you write
>
> @users = User.find(:all)
>
> then in view file you can directly use that array with each loop
>
> @users.each do |c|
> c.title
> end
>
> but in my case
>
> in view file i write same code like for @users but I am getting errr
> like
>
> undefined title
>
> when I use this array
>
> [#,#] [active record array ] it works in view file
>
> 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
>
> @users.each do |c|
> c.title it will not work
> end
>
> instead of that i have to write @users[0].title
>
> got it ? any solution ?
>
>
>
> Frederick Cheung wrote:
> > On Feb 21, 11:11 am, Cyrus Dev <[email protected]>
> > wrote:
> >> thanks fred
>
> >> but in o/p
>
> >> 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.
>
> > Fred
>
> --
> Posted viahttp://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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---