views:

75

answers:

3

Right now I'm building a project management app in rails, here is some background info:

Right now i have 2 models, one is User and the other one is Client. Clients and Users have a one-to-one relationship (client -> has_one and user -> belongs_to which means that the foreign key it's in the users table)

So what I'm trying to do it's once you add a client you can actually add credentials (add an user) to that client, in order to do so all the clients are being displayed with a link next to that client's name meaning that you can actually create credentials for that client.

What i can't figure it out how to do is, that if you actually have credentials in the database (meaning that there's a record in the users table with your client id) then don't display that link.

Here's what i thought that would work

<% for client in @client%>
     <h5>
         <h4><%= client.id %></h4>
         <a href="/clients/<%= client.id %>"><%= client.name %></a>
          <% for user in @user %>
            <% if user.client_id = client.id %>
                <a href="/clients/<%= client.id %>/user/new">Credentials</a>
            <%end%>
          <% end %>
     </h5>
<% end %> 

And here's the controller:

def index
@client = Client.find_all_by_admin(0)
@user = User.find(:all)
end

but instead it just puts the link the amount of times per records in the user table. Any help?

+1  A: 

You can do the following

# controller
def index
   @clients = Client.find_all_by_admin(0, :include => :user)
end

# view
<% @clients.each do |client| %>
   <h5>
      <h4><%= client.id %></h4>
      <%= link_to client.name, {:action => 'show', :id => client.id} %>

      <% if client.user.blank? %>
         <%= link_to "Credentials", 
            {:controller => 'user', :action => 'new', :client_id => client.id} %>
      <% end %>
   </h5>
<% end %> 

Edit

Better solution, getting in the controller only the clients who doesn't have a user yet:

# model
class Client < ActiveRecord::Base
   has_one :user

   named_scope :without_user,
      :conditions => "id NOT IN ( SELECT client_id FROM users )"
end

# controller 
@clients = Client.without_user.find_all_by_admin(0)

# view
<% @clients.each do |client| %>
   <h5>
      <h4><%= client.id %></h4>
      <%= link_to client.name, {:action => 'show', :id => client.id} %>
      <%= link_to "Credentials", 
            {:controller => 'user', :action => 'new', :client_id => client.id} %>
   </h5>
<% end %> 
j.
Thank you so much! The first option was the right answer because i need to display all the clients, what i didn't want to always display was the link. Appreciate the fast and accurate answer.
Gotjosh
I'm glad I could help :]
j.
Do you mind taking a look at the comment below my answer? I'm now struggling with that =(
Gotjosh
+1  A: 

Just to add to j.'s answer, you really should use link_to view helper instead of hardcoding URLs.

bsboris
Thank you! Will do from now on.
Gotjosh
+1  A: 

I haven't worked with Rails in sometime, but two things:

  1. There are helpers to make your urls, it's easy to make mistakes. (Not that I see one, but just to let you know about them...)
  2. Views should not have logic. As much as you can get away put it in the controller. Or even better in the model.
  3. It's a good practice to not use "User.find :all", as your app grows this will kill you.
  4. It's better to have a method in the model that gets just the info you will use. Databases are better in this case to filter.
eipipuz
Thanks for the hits! I really appreciate this kind of comments they make a me improve my rails skills! :)
Gotjosh