Search code examples
mysqlruby-on-railsapimodel-view-controllerreact-on-rails

Is there a way to fetch data in database from a Ruby-on-Rails models


I have a rails app running alongside with a rails API, there is a constant value for DAYS_LIMIT in config/initializers/constants.rb

DAYS_LIMIT = 40
DEFAULT_PRICE = 1.29

but now in the app i added an input field so that the user decide his DAYS_LIMIT. So i want to fetch that value from the database from inside the API models.

I have placed breakpoints and can see that inside the API controller, the data is transfered from the app but not to the models.

edited as a question requested , it's a React-on-Rails app , here is the code where the new input field is save to the database (i have removed the other fields so the question look shorter)

export const saveChannel = (files) => {
    return async (dispatch, getState) => {
        const { channel } = getState();
        const {rss_podcast_days} = channel;
        const { image } = files;

    const save = id ? updateChannel : createChannel;

    const sub_required = subscription_required !== undefined ? subscription_required : false;

        const formData = new FormData();
                formData.append('channel[rss_podcast_days]', rss_podcast_days || '');


    if (Object.keys(image).length) {
      formData.append('channel[image]', image);
    }

    const channelId = await dispatch(save(formData, id));

    dispatch(fetchChannel(id));

    return id;
  };
};

from the app controller

podcast_list = RestClient.get("#{ENV['URL_API']}/api/#{@channel.id.as_json}/podcast/list")
      @podcasts = JSON.parse(podcast_list.body)
      @podcasts = @podcasts.sort.reverse.to_h

this is from the API controller witch the data is transfered from the app

def index
    podcasts = @channel.podcasts.published.list(params[:page], params[:items_per_page], params[:ordered_in])

    render json: Podcasts::Normalizer.normalize(podcasts, @channel.station.default_podcast_price)
  end

and here from the API model that i want to fetch data instead of the constants.

scope :by_days_limit, -> {with_tags.more_recent_than(Date.today - DAYS_LIMIT.days).ordered}

it should take today date minus the value (DAYS_LIMIT) from user input, but for now i get undefined local variable or method if i try to fetch directly


Solution

  • ok , so i finally got it, i don't know if i should delete this thread or just tell how i did it. If it's inapropriate just tell me and i will delete this entire thread.

    So here is how i did it, in the API controller i had to add my fetch so that the arguments (list) knows what i am talking about. @channel.days_limit

    def index
        podcasts = @channel.podcasts.published.list(params[:page], params[:items_per_page], params[:ordered_in], @channel.days_limit)
    
        render json: Podcasts::Normalizer.normalize(podcasts, @channel.station.default_podcast_price)
    end
    

    then in the def list of the models, i added days_limit has argument

    def list(page = nil, nb_items_per_page = 40, ordered_in = 'desc', days_limit)
          ordered_in = ordered_in.in?(['asc', 'desc']) ? ordered_in : 'desc'
          page.blank? ? by_days_limit(days_limit) : by_page(page, nb_items_per_page, ordered_in)
    end
    

    and finally in the scope of the models, i pass in the new argument

    scope :by_days_limit, -> (days_limit) {with_tags.more_recent_than(Date.today - days_limit.days).ordered}
    

    Now the user input from the app is passing to the models via the controller.