0

I am using Ruby on Rails and I would like to render a web page that makes use of database data for promotional purposes. The source code is stated something like the following:

# Model
PROMO_ID = 9

# Controller
@promo_article = Article.find(PROMO_ID)

# View
<%= @promo_article.title %>
<%= @promo_article.content %>

In the above code I "simply" look for an object instance (a promotional one) relating a database record and then I render it in the view. However, by using the above code, I have to ensure the record existence (that having the id equal to that stated for PROMO_ID) as a requisite, otherwise the application may raise errors.

I am in trouble because, in order to make the page to work, the record must be present in both development and production database and its reference PROMO_ID may differ since in development it may be 9 (as in the above code) and in production it may be 378.

I thought of using mock objects for my purposes but I would like to use a database record because this way the "promotional" record can be further processed by users. I am looking for a balanced and lean solution to the issue.

What do you advice about? How should I proceed? Should I state some id constants or use mock objects?

Backo
  • 18,291
  • 27
  • 103
  • 170
  • I've seen this question before, it seems. Edit question to clarify it, don't recreate it. And I still don't get the definition of "promotional content". What logic should it follow? – D-side Dec 25 '14 at 16:46
  • Does only 1 article can be promotional? Should you be able to change promotional article to other one? How do you chose that id? – Rustam Gasanov Dec 25 '14 at 16:47
  • @D-side - The logic is that the promo article'd be used by users as well as for promotional purposes and I would like to have one only article record in the database. – Backo Dec 29 '14 at 12:50
  • @Rustam A. Gasanov - The plan is to have more than one promotional article. Other users should not be able to change that article, only admins can do. I chose that `id` according to the presence of the related record in the database. – Backo Dec 29 '14 at 12:52

2 Answers2

1

Try using something less rigorous than find.

# in model
def self.december_promo
  find_by(title: 'December 2014 Promo') }
end

# in controller
@promo_article = Article.december_promo

find_by will be nil if it does not find the record.

Justin M
  • 1,065
  • 2
  • 10
  • 15
  • I usually try to avoid hard-coding IDs into the application itself. What if production and staging fall out of sync? If a human has to play with IDs, consider making a [surrogate ID column](http://en.wikipedia.org/wiki/Surrogate_key). If this particular feature is very well-controlled, though, ID can do fine do in a pinch. – Justin M Dec 29 '14 at 15:53
0

Simple use Case expression

case Rails.env
  when "development"
    PROMO_ID = 9
  when "production"
    PROMO_ID = 378
end
Nithin
  • 3,679
  • 3
  • 30
  • 55
  • Yes, I know. But there is some more underlying (e.g., certainly some drawbacks)... I am asking even for that other than the solution. – Backo Dec 25 '14 at 15:15