#gem ''rails'', ''3.0.9'' #gem "paperclip", "~> 2.0" rails console>contract = Contract.first=> #<Contract id: 1, . . .>files = contract.contract_files=> [#<ContractFile id: 1, contract_id: 1, . . .>files.first.data.url=> "/system/data/1/original/logo.png?1325513082"> files.map(&:data_file_name).each do |key| key end=> ["logo.png", "newyearseve-2011-res.jpg", nil] #IT WORKS FINE!> files.map(&:data.url).each do |key| key end=> NoMethodError: undefined method `url'' for :data:Symbol #IT DOESN''T WORK Can you explain how to get access to "data.url" in has_many model? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Well, you can''t because you''re not calling an instance method on every element of your array. data_file_name works because each ContractFile object has an attribute called data_file_name. So, you can call map() on files and collect every filename by specifying data_file_name. In contrast, url is an instance method you have to call on every PaperClip attachment and you can''t give it as a symbol. You have to specify it this way:> files.collect { |f| f.data.url }2012/1/2 Nikolay <itsnikolay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> #gem ''rails'', ''3.0.9'' > #gem "paperclip", "~> 2.0" > > rails console > >contract = Contract.first > => #<Contract id: 1, . . . > >files = contract.contract_files > => [#<ContractFile id: 1, contract_id: 1, . . . > >files.first.data.url > => "/system/data/1/original/logo.png?1325513082" > > files.map(&:data_file_name).each do |key| key end > => ["logo.png", "newyearseve-2011-res.jpg", nil] > #IT WORKS FINE! > > > > files.map(&:data.url).each do |key| key end > => NoMethodError: undefined method `url'' for :data:Symbol > #IT DOESN''T WORK > > Can you explain how to get access to "data.url" in has_many model? > > > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Jan 2, 2012 at 10:02 PM, Nikolay <itsnikolay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> #gem ''rails'', ''3.0.9'' > #gem "paperclip", "~> 2.0" > > rails console > >contract = Contract.first > => #<Contract id: 1, . . . > >files = contract.contract_files > => [#<ContractFile id: 1, contract_id: 1, . . . > >files.first.data.url > => "/system/data/1/original/logo.png?1325513082" > > files.map(&:data_file_name).each do |key| key end > => ["logo.png", "newyearseve-2011-res.jpg", nil] > #IT WORKS FINE! > > > > files.map(&:data.url).each do |key| key end > => NoMethodError: undefined method `url'' for :data:Symbol > #IT DOESN''T WORK > > Can you explain how to get access to "data.url" in has_many model?Hmm.. to my knowledge, & (unary operator) is converting to_proc on an object. so file.map(&:data_file_name) is equal to: file.map {|i| i.data_file_name) but on your second example files.map(&:data.url) , ruby interpret it as call ''url'' method on symbol :data and call to_proc hmm. I dont know the solution, but you might try files.map do |file| file.data.url end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you, Ahmy! Simple solutions always work fine. That''s all we need. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.