~MODEL~
../contract.rb
class Contract < ActiveRecord::Base
has_and_belongs_to_many :places
end
../place.rb
class Place < ActiveRecord::Base
  has_and_belongs_to_many :contracts
end
../schema.rb
  create_table "contracts", :force => true do |t|
     . . .
       t.string   "place"
     . . .
  end
  create_table "contracts_places", :id => false, :force => true
do |t|
       t.integer "contract_id"
       t.integer "place_id"
  end
  create_table "places", :force => true do |t|
       t.string   "name"
       t.datetime "created_at"
       t.datetime "updated_at"
  end
                                             ~CONTROLLER~
../contracts_controller.rb
  def show  # (edit)
    @contract = Contract.find(params[:id])
     @places = @contract.places
  end
                                             ~VIEW~
../_contract.html.erb
<%= @places.names %> # Does''t work
__________________________________________________________________
                                                 ~CONSOLE VERSION~
Rails db:
sqlite> select * from contracts_places;
1|1
1|2
1|3
7|1
7|2
Rails console version:
ruby-1.9.2-p290 :001 > contract = Contract.first
 => #<Contract id: 1, place: "1">
ruby-1.9.2-p290 :002 > place = contract.places
 => [#<Place id: 1, name: "Street1", created_at: "..",
updated_at:
"..">,
       #<Place id: 2, name: "Street2", created_at: "..",
updated_at:
"..">,
       #<Place id: 3, name: "Street3", created_at: "..",
updated_at:
"..">]
ruby-1.9.2-p290 :003 > place.names
       NoMethodError: undefined method `names'' for #<Class:
0x00000002025150>
ruby-1.9.2-p290 :004 >
                                          ~Question~
How to output all list of places?
-- 
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 Sun, Dec 25, 2011 at 7:48 PM, Nikolay <itsnikolay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ~VIEW~ > ../_contract.html.erb > <%= @places.names %> # Does''t work@places.map(&:name) will give you an array of names which you can format as you wish. HTH, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for help. It works fine! On 26 дек, 20:49, Hassan Schroeder <hassan.schroe...@gmail.com> wrote:> On Sun, Dec 25, 2011 at 7:48 PM, Nikolay <itsniko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > ~VIEW~ > > ../_contract.html.erb > > <%= @places.names %> # Does''t work > > -WBar1Xirz0eWJ+JfZevqRA@public.gmane.org(&:name) > > will give you an array of names which you can format as you wish. > > HTH, > -- > Hassan Schroeder ------------------------ hassan.schroe...-Re5JQEeQqe80Tx58lXaADg@public.gmane.org://about.me/hassanschroeder > twitter: @hassan-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.