1

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_user_nickname", 
      :group => 'vouched_user_nickname'                  

)

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

5 Answers 5

3

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 %>
1
  • Yay. that worked. really elegant and creative solution. thank you.
    – user250181
    May 24, 2010 at 23:35
0
@vouch =  Vouch.find_by_vouched_user_nickname(:all, ["nickname1","nickname2"])
3
  • I tried that but it's not working. Do I need to change the view? Thanks.
    – user250181
    May 24, 2010 at 22:02
  • Any other advice? I tried this in a few ways, it's not working.
    – user250181
    May 24, 2010 at 22:52
  • You would need to do something different with @vouch(and maybe rename it to vouches) now, since it is an array, like @vouches.each do |vouch|... in the view. May 24, 2010 at 23:08
0

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 %>
1
  • This would repeat each tweet for each vouch in the database. Definitely not wanted.
    – Samuel
    May 24, 2010 at 23:25
0

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}
0

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:

3
  • I am a noob so for the sake of simplicity, both those problems relate.
    – user250181
    May 24, 2010 at 22:05
  • hmm, actually, I think my problem is that I don't know what to do with the array in the view. It seems to be grabbing the right data.
    – user250181
    May 24, 2010 at 22:07
  • hmmm. that's not working either. I updated the main question with all the code in the view and the controller code. Am I missing something?
    – user250181
    May 24, 2010 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.