2

I have an array called @venues with all the restaurants within a radius. Each venue has_manydishes and I want to insert an array of these dishes into venue.

@venues = Venue.within(radius, :origin => [lat, lng]).order('distance ASC')

@venues.each do |venue|
  dishes = venue.dishes.where("? BETWEEN DATE(served_from) AND DATE(served_until)", Date.today)
  # insert dishes into venue
end

How do I insert dishes into venue so that I can access all the available dishes of an venue with: @venues[i].dishes?

1 Answer 1

4

It might might more sense to add a scope on Dish itself:

scope :today, :conditions => ["? BETWEEN DATE(served_from) AND DATE(served_until)", Date.today]

With that, you can access the available dishes with @venues[i].dishes.today

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

1 Comment

Agreed. It might be useful though to clarify why what they're doing is a bad idea: they're mutating the state of each Venue instance (by assigning dishes to a subset of itself) with no intent to save those changes because they're not actually correct.

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.