Created
February 20, 2016 18:34
-
-
Save drakmail/ab5373a6da57e96b17eb to your computer and use it in GitHub Desktop.
STI example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DrinkHabit < Habit | |
# DB structure | |
# id:integer | |
# title:text | |
# food_amount:integer | |
# drink_time:datetime | |
# type: string | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FoodHabit < Habit | |
# DB structure | |
# id:integer | |
# title:text | |
# food_amount:integer | |
# drink_time:datetime | |
# type: string | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Habit < ActiveRecord::Base | |
# DB structure | |
# id:integer | |
# title:text | |
# food_amount:integer | |
# drink_time:datetime | |
# type: string | |
belongs_to :user | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HabitsController < ApplicationController | |
def update | |
habit = Habit.find(params[:id]) | |
if habit.update habits_params | |
redirect_to habit_path(habit) | |
else | |
render 'show' | |
end | |
end | |
protected | |
def habits_params | |
params.require(:habit).permit(:id, :title, :food_amount, :drink_time) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for @habit do |f| %> | |
<%= f.text_field :title %> | |
<%= if @habit.is_a? FoodHabit %> <!-- better to use rendering of partials and helper --> | |
<%= f.text_field :food_amount %> | |
<% end %> | |
<% if @habit.is_a? DrinkHabit %> | |
<%= f.text_field :drink_time %> | |
<% end %> | |
<%= f.submit %> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment