sql - Adding a column Migration with a default value in ruby on rails -
i using sqlite3 , following work:
class addnametogoal < activerecord::migration def change add_column :goals, :goal_name, :text, default: goal.exercise.name end end
or maybe makes more sense i'm trying do:
add_column :goals, :gname, :text, default: goal.find(row_id).exercise.name
how above work.
i doubt work that's want.
specifically, user associated exercise through exercise_id column.
belongs_to :user belongs_to :exercise has_many :workouts, dependent: :destroy
(this model goal)...
i user able choose own name goal can give them hint name goal after exercise's name , if choose leave blank default exercise's name. more importantly must happen on sql side later when have collection drop down requires name of goal need name corresponds exercise.
<%= f.collection_select(:goal_id, @goals, :id, :goal_name, :include_blank => "please select") %>
the exercise model made in rails have id, name, other columns.
exercise model:
class exercise < activerecord::base has_one :goal
is there strategy possible.
another option me find strategy active record can do:
<%= f.collection_select(:goal_id, @goals, :id, :goal_name, :include_blank => "please select") %>
with (something else replace goal_name exercise.name goal.exercise.name , goal.id , show id.
doing when define column on table problematic. think doing upon creation, in model, how you'd want go.
you might check out the default_value_for
gem , see if helps.
Comments
Post a Comment