Topic: What to compare on this case statement

I haven't used case statements before and was wondering how to do the following.

Scenario

I have a number of news pages, each having posts relevant to department, so the pages

    /tynewyddnews
    /woodsidenews
    /outreachnews
    /sandpipernews

On my home page I am grabbing the latest post for each of these and displaying them.


Post Model


    def self.top_posts
    #Array with each of the 4 departments - first record
    top_posts = [
      self.tynewydd_posts.first,
      self.woodside_posts.first,
      self.sandpiper_posts.first,
      self.outreach_posts.first
    ]
    #remove entry if nil
    top_posts.delete_if {|x| x==nil}
    return top_posts
    end

tynewydd_posts for example is a scope

 
    scope :tynewydd_posts, :include => :department, :conditions => {"departments.name" => "Ty Newydd"}, :order => "posts.published_on DESC"

So if i am reading a post on the home page from tynewydd and want to create a link to /tynewyddnews or i am reading a post from woodside and want to link_to /woodside I have been advised that a case statement may help, but I am unsure on what to use as parameters, so my attempt so far is

    def public_news
     path = case
     when Post.tynewydd_posts then tynewyddnews_path
     when Post.woodside_posts then woodsidenews_path
     ...
    else default_path end
   end
   link_to('...Read More', path)
end

And then in my view i can call the helper

    <%= public_news %>

But this isnt working, every link is the same (tynewydd for example).Im not sure if has something to do with the way I am iterating through the results?

    <% @toppost.each do |t| %>
    <div class="post-item">
      <h2><%= t.title %></h2>
       <ul id="newsHome">
        <li><%= date_output(t.published_on) %></li>
        <li><%= t.department.name %></li>
        <li>by Admin</li>
       </ul>
       <% if t.comments.length > 350 %>
        <p class="post-description"><%= truncate(t.comments, length: 350).html_safe %>
        <%= public_news %></p>
        <% else %>
        <p class="post-description"><%= t.comments.html_safe %></p>

       <% end %>
    </div>
   <% end %>


if someone could give some advice on how to achieve this it would be much appreciated

Re: What to compare on this case statement

try

def public_news
     case path
     when 'Post.tynewydd_posts' then tynewyddnews_path
     when 'Post.woodside_posts' then woodsidenews_path
     ...
    else default_path end
   end
   link_to('...Read More', path)
end

cheers

Re: What to compare on this case statement

Hi

Thanks for your answer, big help

Re: What to compare on this case statement

Glad to help when I can ^_^