Topic: view, ids & array

Hi, How do i do this?
I have an array of cities I am emulating in the view, the problem is they need to link to their id, i can't figure out how to do it?

in the controller...
     @users_cities = Profile.find(p.user.profiles).collect {|p| p.city}
     @city = @users_cities - [p.city]

in the view...
     <%= @city.collect { |p| link_to p,
     :controller => "profiles", :action => "show",:id => @city_id}.join(" & ") %>
#@city_id ???? How ???? I am stumped.


Sorry, I'm a bit of a noob.

Re: view, ids & array

It looks like you have three models here: user, profile, and city. Can you post the model associations you use for each of these?

Railscasts - Free Ruby on Rails Screencasts

Re: view, ids & array

Yep sorry,

class Profile < ActiveRecord::Base
    has_many :accounts, :dependent => :destroy
    has_many :users, :through => :accounts

class User < ActiveRecord::Base
    has_many :accounts, :dependent => :destroy
    has_many :profiles, :through => :accounts

class Account < ActiveRecord::Base
    belongs_to :user
    belongs_to :profile


City is a field/ attribute for profiles.

Re: view, ids & array

I recommend just grabbing the profiles in the controller like this:

@profiles = p.user.profiles

And then looping through those in the view:

<% for profile in @profiles %>
  <%= link_to profile.city, :controller => "profiles", :action => "show", :id => profile %>
<% end %>

Is that what you want?

Railscasts - Free Ruby on Rails Screencasts

Re: view, ids & array

Yes, Thankyou, but i will then loose the functionality of returning all but the

p.user.profiles, 'city' where @profile(current)

@city = @users_cities - [p.city]

and also the ampersands in between with the .join (" & ") method.

Re: view, ids & array

Sorry,  Bump!