我有两个型号Group和Person.两者都是HABTM,但我希望一个人能够在不删除该组的情况下退出特定组,而只是该组中的人.我一直在试图在控制台中解决这个问题,我似乎无法得到它.我将展示我的代码以便澄清.
我现在正在制作这种方法
def delete_from_group(phone_number)
person = Person.find_by(phone_number: phone_number)
person.groups.destroy
end
Run Code Online (Sandbox Code Playgroud)
这是我的控制器.
def create
# Grab the phone number from incoming Twilio params
@phone_number = params[:From]
# Find the subscriber associated with this number or create a new one
@subscriber = Person.find_or_create_by(phone_number: @phone_number)
@delete_group = Person.find_by(phone_number: @phone_number)
# Update location data
@subscriber.update(
city: params[:FromCity],
state: params[:FromState],
zip: params[:FromZip],
country: params[:FromCountry]
)
@body = params[:Body].to_s.downcase.strip
begin
# Process the command from our Subscriber
output = process_message(@body, @subscriber, @delete_group) …Run Code Online (Sandbox Code Playgroud) 我需要创建一种方法,将创建日期过去一年的日期呈现到表中。我已经尝试过标题中列出的行,但没有成功。我现在有一个表格,旁边列出了“加入日期”,我希望它显示“日期已过期”。自加入之日起一年。
例子:
class Subscriber < ActiveRecord::Base
validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :phone_number, presence: true
def date_joined
created_at.strftime("%-m/%-d/%-y")
end
def expiration_date
created_at.1.year.from_now
end
end
Run Code Online (Sandbox Code Playgroud)
我应该如何格式化该expiration_date方法。date_joined 工作正常。
我有点荒谬但我真的不知道这个错误试图告诉我什么?我是rspec的新手,对我来说这是一个新手.为了清楚起见,我将发布我的代码 - 错误:未定义的方法`permit'为"1":字符串|
控制器:
def subscriber_params
params.require(:subscriber).permit(:first_name, :last_name, :email, :phone_number)
end
Run Code Online (Sandbox Code Playgroud)
SPEC:需要"rails_helper"
describe SubscribersController do
include Devise::TestHelpers
let(:user) { FactoryGirl.create(:user) }
let(:subscriber) { FactoryGirl.create(:subscriber) }
it "creates a new comment" do
sign_in(user)
comment = FactoryGirl.attributes_for(:comment)
expect { post :create, subscriber: subscriber, comment: comment }.to change(Comment, :count).by(1)
end
end
Run Code Online (Sandbox Code Playgroud)
错误:
我正在尝试更新属性.它是一个默认值为false的布尔值.在我的方法中,我进行查询以找到合适的用户.然后我尝试通过将其翻转owner boolean为true 来更新该用户.这是一个非常奇怪的错误,因为它正在更新方法,但它也发出了这个错误TypeError: nil is not a symbol nor a string
我只是想知道我在这里做错了什么.
def update
membership = current_account.account_memberships.find_by(user_id: params[:id])
membership.update(owner: true)
end
Run Code Online (Sandbox Code Playgroud)
<%= link_to "Owner", account_membership_path(user), {
class: "icon icon-owner-upgrade",
method: :patch,
} %>
Run Code Online (Sandbox Code Playgroud) 如果条件为假,我正在尝试在输入中打印单词NO.这是我目前正在尝试的
=IF (A2="a")AND(b2="b", "YES","NO")
Run Code Online (Sandbox Code Playgroud)
看起来这会工作,但我一直得到这个错误| Formula parse error.
我是EXCEL的新手,我不知道我做错了什么?任何帮助都会很棒谢谢!
我有一个标准DateTime数据类型的starts_at 属性。我想利用该时间和当前时间来计算3距离活动还剩多少天。这是我目前的方法
def days_remaining
(Date.parse(starts_at) - Date.current).to_i
end
Run Code Online (Sandbox Code Playgroud)
但这给了我这个错误no implicit conversion of ActiveSupport::TimeWithZone into String
这看起来很简单,但我似乎无法弄清楚。
我正在尝试将Timex模块格式化为某种方式.我想要今天的约会.但我希望它的格式如下:
2017年12月12日.
年/分钟/天
在ruby中我会去strftime类,但我不知道如何使用Elixir:
目前的尝试:
Timex.local => #DateTime<2017-12-12 19:57:17.232916-05:00 EST America/Detroit>
Run Code Online (Sandbox Code Playgroud)
我该如何处理并格式化我指定的方式?
我正在尝试查找有多少项目属于特定用户.
我知道你可以这样做以获得模型的计数:
count = User |> Repo.aggregate(:count, :id)
Run Code Online (Sandbox Code Playgroud)
但是,如何找到属于该用户的项目数?
If I have two lists:
First list => [1,2,3]
Second List => [3,4,5]
I want this to return true because they both contain 3? If 3 was replaced by 6 it would return false because no elements would match.
Current Attempt
Enum.member?(first_list, fn(x) -> x == second_list end)
Run Code Online (Sandbox Code Playgroud)
Ecto queries:
user_teams = from(
t in MyApp.Team,
left_join: a in assoc(t, :accounts),
where: p.owner_id == ^user.id or (a.user_id == ^user.id and t.id == a.project_id)
) |> Repo.all
current_user_teams = from( …Run Code Online (Sandbox Code Playgroud) 这是我的模块:
defmodule Test do
def try(10 = num, other_num \\ 10) do
num + other_num
end
def try(_num, _other_num) do
raise "This is incorrect"
end
end
Run Code Online (Sandbox Code Playgroud)
当我跑步时,iex我得到这个警告:
warning: def try/2 has multiple clauses and also declares default values. In such cases, the default values should be defined in a header. Instead of:
def foo(:first_clause, b \\ :default) do ... end
def foo(:second_clause, b) do ... end
one should write:
def foo(a, b \\ :default)
def foo(:first_clause, b) do …Run Code Online (Sandbox Code Playgroud)