0

I am unsure how to call a model method from a controller

In Event controller query is ran in

 class StaticPagesController < ApplicationController
    def self.all_published()
       events = 'SELECT  user_id FROM events'
       where("dotw = DAYNAME(curdate())")
    end
  end

Page_controller

  def index
    @published_posts = Event.all_published.paginate()
  end

View

  <% if @published_posts.any? %>
     <ol class="microposts">
       <%= render partial: 'shared/feed_item', collection: @published_posts %>
    </ol>
    <%= will_paginate @published_posts %>
 <% end %>

1 Answer 1

1

Rails follow the "fat model, skinny controller" convention. This means that you should put your business logic, such as queries, inside your models.

In your case, the all_published method should go in your Event model such as:

class Event
  def self.all_published
    where('your sql query here')
  end
end

Then call with:

Event.all_published

Rails controllers should only be responsible of rendering / redirecting and settings views variables.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.