开发者

How do I run an array through an IF statement in rails?

开发者 https://www.devze.com 2022-12-31 17:21 出处:网络
I am creating an application that highlights user messages from a stream based on whether or not the user has been \'vouched\'. It works fine if it\'s setup for a single author. For example

I am creating an application that highlights user messages from a stream based on whether or not the user has been 'vouched'. It works fine if it's setup for a single author. For example

controller: @vouch = Vouch.last.vouched_user_nickname

view:

 <% Twitter::Search.new(params[:id]).each do |tweet| %>
   <li>
     <%= image_tag tweet.profile_image_url %> 

<% if @vouch.include? tweet.from_user %> <div class="flit_message_containerh">

            <u> <a href="http://twitter.com/<%= tweet.from_user %>"> <%= tweet.from_user %></a></u> <%= linkup_mentions(auto_link(h tweet.text)) %>
            <div class="time_ago">
          <%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
       <% else %>    <div class="flit_message_container">
    <u> <a href="http://twitter.com/<%= tweet.from_user %>"> <%= tweet.from_user %></a></u>
           <%= linkup_mentions(auto_link(h tweet.text)) %>
                <div class="time_ago">
              <%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
            <% end %>

But I'm having trouble doing it for multiple user nicknames.

@vouch =  Vouch.find(:all,
      :select => "vouched_us开发者_如何学运维er_nickname", 
      :group => 'vouched_user_nickname'                  

)

Any ideas would be greatly appreciated. I'm a rails noob.


Assuming there isn't a relation between your Vouch model and the Twitter source (I haven't used that gem/plugin yet so I don't know), one solution is to pull all the Twitter entries you want and all the vouches in the controller and do the check in the view.

controller:
@tweets = Twitter::Search.new(params[:id])
@vouches = Vouch.find(:all)

view:

<% @tweets.each do |tweet| %>
  <div class="flit_message_container<%=
    @vouches.any? { |v| v.vouched_user_nickname == tweet.from_user } ? "h" : ""
  %>">
    ...
  </div>
<% end %>


@vouch =  Vouch.find_by_vouched_user_nickname(:all, ["nickname1","nickname2"])


Your problem seems to be that you are not looping through the array, so how can it decide if certain elements meet the criteria you set?

Example, in your view:

<% for vouch in @vouch do %>
  <% if vouch.include? tweet.from_user %>
    <div class="flit_message_containerh">
      <u><a href="http://twitter.com/<%= tweet.from_user %>"> <%= tweet.from_user %></a></u> <%= linkup_mentions(auto_link(h tweet.text)) %>
            <div class="time_ago">
          <%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
       <% else %>    <div class="flit_message_container">
    <u> <a href="http://twitter.com/<%= tweet.from_user %>"> <%= tweet.from_user %></a></u>
           <%= linkup_mentions(auto_link(h tweet.text)) %>
                <div class="time_ago">
              <%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
  <% end %>
<% end %>


I see several problems. The first one is this:

@vouch = Vouch.last.vouched_user_nickname

You are using a variable called @vouch to store a user nickname. That is counterintuitive and will confuse other people reading your code (like myself). Use something like this instead:

@vouch = Vouch.last #on the controller
@vouch.vouched_used_nickname #on the view

This ... eum ... "exotic" naming convention helps confusing yourself when you try to do the "multiple" example:

@vouch =  Vouch.find(:all,
      :select => "vouched_user_nickname", 
      :group => 'vouched_user_nickname')

Activerecord's find(:all, ...) will allways return an array of activerecord objects (or an empty array). You seem to be expecting an array of strings. You will allways get Vouches if you do Vouch.find.

The :select part just limits the amount of information these vouches have (they only come with vouched_user_nickname populated. The rest, including their id, is empty, because it is not read from the database).

If you want to have an array of user nicknames you can do it like this:

# note the names. @vouchers in plural, and @nicknames for the user names
@vouchers =  Vouch.find(:all, :select => "vouched_user_nickname",
  :group => 'vouched_user_nickname')
@nicknames = @vouchers.collect{|v| v.vouched_user_nickname}


Is your problem that you don't know the correct controller code to write to find the @vouch array? Or is it that you don't know what to do with the array once you get it?

 view: <% if @vouch.include? tweet.from_user %> 

.include? is a method you can call on either a single object or an array of objects if tweet.from_user has an object that is also included in the @vouch array to get the @vouch array in your controller you should:

0

精彩评论

暂无评论...
验证码 换一张
取 消