【问题标题】:Is there a way to fetch data in database from a Ruby-on-Rails models有没有办法从 Ruby-on-Rails 模型中获取数据库中的数据
【发布时间】:2019-04-03 19:21:59
【问题描述】:

我有一个与 rails API 一起运行的 rails 应用程序,在 config/initializers/constants.rb 中有一个 DAYS_LIMIT 的常量值

DAYS_LIMIT = 40
DEFAULT_PRICE = 1.29

但现在我在应用程序中添加了一个输入字段,以便用户决定他的 DAYS_LIMIT。 所以我想从 API 模型内部的数据库中获取该值。

我已经放置了断点,并且可以看到在 API 控制器内部,数据是从应用程序而不是模型传输的。

根据请求的问题进行编辑,它是一个 React-on-Rails 应用程序,这是将新输入字段保存到数据库的代码(我已删除其他字段,因此问题看起来更短)

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

来自应用控制器

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

这是来自 API 控制器,数据是从应用程序传输的

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

我想从 API 模型中获取数据而不是常量。

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

它应该取今天的日期减去用户输入的值 (DAYS_LIMIT),但现在如果我尝试直接获取,我会得到 undefined local variable or method

【问题讨论】:

  • 能否将保存字段的代码添加到数据库中
  • 是否保存为模型中的属性?
  • @Mark 问题已编辑

标签: mysql ruby-on-rails api model-view-controller react-on-rails


【解决方案1】:

兄弟,如果你的类有像 DAYS_LIMIT 这样的常量,你可以使用该类本身来访问它,例如,

class Demo
  DAYS_LIMIT = 5
end

您可以通过控制器中的Demo.DAYS_LIMIT 或任何您需要的地方访问该常量。

祝你好运!

【讨论】:

    【解决方案2】:

    好的,所以我终于明白了,我不知道我是否应该删除这个帖子或者只是告诉我是怎么做到的。如果有不当之处,请告诉我,我将删除整个帖子。

    这就是我的做法,在 API 控制器中我必须添加我的 fetch 以便参数(列表)知道我在说什么。 @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
    

    然后在模型的 def 列表中,我添加了 days_limit 有参数

    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
    

    最后在模型的范围内,我传入了新参数

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

    现在来自应用的用户输入通过控制器传递给模型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多