简体   繁体   中英

Adding a column Migration with a default value in ruby on rails

I am using SQLite3 and I would like the following to work:

class AddNameToGoal < ActiveRecord::Migration
  def change
    add_column :goals, :goal_name, :text, default: goal.exercise.name
  end
end

Or maybe this makes more sense as what I'm trying to do:

add_column :goals, :gname, :text, default: Goal.find(row_id).exercise.name

How do I get the above to work.

I doubt it will work as it is but that's what I want.

Specifically, The user is associated with an Exercise through the exercise_id column.

belongs_to :user
belongs_to :exercise
has_many :workouts, dependent: :destroy

(This is the model for Goal)...

I would like the user to be able to choose their own name for the Goal but I can give them the hint to name the goal after the Exercise's name and if they choose to leave it blank it will default to the exercise's name. More importantly this must happen on the SQL side so that later when I have a collection drop down which requires a name of the goal they will need a name which corresponds to the exercise.

<%= f.collection_select(:goal_id, @goals, :id, 
:goal_name, :include_blank => "Please Select") %>

The Exercise Model is made in Rails to have id, Name, other columns.

Exercise Model:

class Exercise < ActiveRecord::Base
    has_one :goal

Is there a strategy by which that is possible.

Another option would be to help me find a strategy for active record so that I can do:

<%= f.collection_select(:goal_id, @goals, :id, 
:goal_name, :include_blank => "Please Select") %>

with (something else to replace goal_name with Exercise.name like goal.exercise.name and goal.id and show only the ID.

Doing this when you define the column on the table is problematic. I definitely think doing it upon creation, in the model, is how you'd want to go.

You might also check out the default_value_for gem and see if it helps.

Hi so in case any body ever wants to do this:

I figured out a strategy...

This is perfect for anything that has an instance of name in terms of the user.

For example, you have something that tracks your Car and you want to give that Car by default the name which comes from that Car's Make_and_Model (model) in Rails.

Naturally when someone says they have a new "Honda Accord" then they get to have that name, but if they ever want to change it to "Lucy" because her name is Lucy and you better treat her with the respect she deserves!, then this gives you the option to do that.

You do not want to change the name for that whole Make&Model you only want to change the name for that specific car.make_and_model which belongs_to User .

If you are wanting something to have a name that defaults to another name but allows the user to change that. Do that on the model level... by setting a before_save method inside the model... like so:

before_save :default_values

  def default_values
    self.goal_name = self.exercise.name if self.goal_name.nil?
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM