0

I'm having the following models:

class Price < ActiveRecord::Base
  belongs_to :article, inverse_of: :prices
  validates :article_id, presence: true
end

class Article < ActiveRecord::Base
  has_many :prices, dependent: :destroy, inverse_of: :article
end

The code when creating them raises validation error when saving (Prices is invalid):

article = Article.new
article.prices.build( { amount: 55.0 } )
article.save! #=> Validation failed: Prices is invalid

So Rails isn't smart enough to save the parent object (Article) before the child objects (Prices) so article_id can be assigned to the price before it is saved.

How do you use validations on the foreign key when using the build function?

It seems like a pretty standard scenario that should work?

(I know you can use database level constraints, but I'm asking about application level validations here)

thejaz
  • 2,763
  • 2
  • 26
  • 40
  • Can you not save the article before building the prices? – David Aldridge Sep 13 '13 at 11:34
  • Yes, that's a workaround. But this seems to be a problem in Rails as this workflow should work. – thejaz Sep 13 '13 at 11:44
  • Possible duplicate of http://stackoverflow.com/questions/5689888/rails-validate-presence-of-association – Marcelo De Polli Sep 13 '13 at 12:40
  • @depa nope, completely different – thejaz Sep 13 '13 at 14:01
  • Care to explain why? You want to validate the presence of a `has_many` association and that is **exactly** what the question I linked asked. – Marcelo De Polli Sep 13 '13 at 14:05
  • Please read the whole question. I'm doing validation already, but the problem is the validations in combination with using the build function. And I'm not mentioning anything about validating a has_many association? I have the validation on the belongs_to as you can see in the example. – thejaz Sep 13 '13 at 14:23
  • Since you're not interested in clearing up your question, I'm voting to close this as off-topic. Also, please don't imply that I haven't read what you wrote because I did that over and over and it still makes no sense. – Marcelo De Polli Sep 13 '13 at 14:41
  • It makes perfect sense to me therefore voting it up – techvineet Sep 13 '13 at 15:28
  • @depa I just thought you didn't read it because it wasn't that complicated. I get a validation error, but Rails should be able to store the objects in the right order to avoid the validation error. – thejaz Sep 13 '13 at 16:56

1 Answers1

0

In Rails way you can do like this

class Article < ActiveRecord::Base
  has_many :prices, dependent: :destroy, inverse_of: :article
  validates_associated :prices
end

but this is not 100% solution to this.

You can try this gem https://github.com/perfectline/validates_existence

techvineet
  • 5,041
  • 2
  • 30
  • 28
  • What does validates_associated :prices do if you say its not the 100% solution? Regarding the gem, I don't want to validate the presence of the actual object - only if an artice_id is present. – thejaz Sep 13 '13 at 11:43
  • It will validate prices from Articles.save but since you are looking for prices#article_id presence, it cannot be achieved even with this. Validate existence tends to give some solution for this. – techvineet Sep 13 '13 at 11:46
  • Didn't deserve vote down. Please use ur right to vote down wisely – techvineet Sep 13 '13 at 15:24
  • It says on the site that you should press the down vote button when the answer is not useful. And your answer is not answering the question, so I can't find a better situation to down vote? I only want other visitors know that it isn't the answer to the question, nothing personal. – thejaz Sep 13 '13 at 16:53