i''m in the process of learning rails and have setup a recipe app in
order to do so.
i have 2 tables, recipes and categories. the recipe model is
''belongs_to :category'' and the category model is
''has_many :recipes''.
i have 2 fields relating to category in the new and edit recipe forms,
a select listing all existing categories named recipe[category_id] and
another field called category[name] so that the user can choose to
enter a new category if they don''t see one in the list if existing
categories.
as far as validation goes, i''d like to allow the recipe[category_id]
field to be blank as long as the category[name] field is not blank but
i am not sure how to do this.
i also have another question. keeping in mind the above form setup
and since there is an association between recipe and category, when a
user decides to enter a new category, can i do something like:
if @params[:recipe][:category_id] != ''''
@recipe.category = Category.new(@params[:category])
end
@recipe.save
will this save the new category due to the association between recipe
and category or do i have to do a @recipe.category.save first?
any help would be appreciated
thanks
Chris