When defining column_names in a counter_culture method, is it possible to deeply associate? In the docs and examples its always an attribute belonging to the model that is used to determine column_names. But, what if the attribute belongs to an associated model?
example, this works
# Account model
counter_culture :user,
column_name: Proc.new { |account| account.has_billable_hours? ? 'billed_hours_sum' : nil },
delta_column: 'billed_hours',
column_names: { ["account.billed_hours > ?", 0] => "billed_hours_sum" }
associated example
# Account model
counter_culture :user,
column_name: Proc.new { |account| account.clients.has_billable_hours? ? 'billed_hours_sum' : nil },
delta_column: 'billed_hours',
column_names: { ["accounts.clients.billed_hours > ?", 0] => "billed_hours_sum" }
If, for the above, you could (you can't) use joins in the column_names method it would look like this
joins(:account=>:client).where("accounts.clients.billed_hours > ?", 0)
The second example illustrates my question. How do you define the column_names when the attribute you need to evaluate does not belong to the parent model, but an associated model?
column_names is only need when calling counter_culture_fix_counts. So, I just 86'ed the column_names option from the the method call and created a rake task to update that counter/column manually.
Related
I have this filter written that i am passing to my endpoint written in Loopbackjs.
const filter = {
where: { status: { neq: "processed" } },
include: [
{ relation: "vendor", scope: { fields: ["id", "name"] } },
{
relation: "pickupLocation",
scope: { where: { cityId: "5ee134da0138634c27a6e1dd" } },
},
],
limit: 200,
skip: 0
};
return this.http.get<IPickupRequest[]>(
`${environment.url}/PickupRequests?filter=${encodeURIComponent(
JSON.stringify(filter)
)}`
);
The pickupRequest collection contains a belongsTo relation to pickupLocation collection. The pickupLocation collection contains a property called cityId on which i am trying to apply a where clause but it does not work. Moreover I also want to get the fields of pickupLocation too inside the pickupRequest object so that i cna use them in my table. The simple include works perfectly and shows the data as desired but when where clause is applied it just doesn't work and also shows only pickupRequest object but does not include pickupLocation data inside it. Moreover since i am using pagination I need it to return me exactly 200 records each time. Am i doing something wrong? What is the exact issue here? I am using Loopback 3
Here, you are expecting that where filter applied on the pickupLocation relation would act something similar to SQL's inner join. However, in loopback, whenever you do an include in the query, the framework internally makes two separate queries on collections or tables of each of those models and combine them; effectively making it work like SQL's left outer join. So, in the above query PickupRequest list will have all object, irrespective of where filter on pickupLocation model. The pickupLocation relation would be present only in those PickupRequest records where cityId = "5ee134da0138634c27a6e1dd" and remaining places, the relation would be an null object.
To overcome this problem, I would say, make your primary query on pickupLocation model as per the { cityId: "5ee134da0138634c27a6e1dd" } where filter and do an include of PickupRequest model.
I have two entities (there is no relation between the two entities): A: Relation and B: Post.
I get every post from the current user to display them on the homepage with this request:
public function getPostsCurrentUser($currentUser)
{
$qb = $this->createQueryBuilder('p');
$qb->where('p.member = :member')
->setParameters(['member' => $currentUser])
->orderBy('p.created', 'DESC');
return $qb->getQuery()->getResult();
}
In the example "A: Relation" the user 2 follow the user 1. When a user follow an other user, I would like to also display on the homepage every post of users that the current user are following.
I don't see how to create this request. Can anybody help me ?
Thanks
In the sense of http://sqlfiddle.com/#!9/c5a0bf/2, I would suggest something like the following which uses a subquery to select the ids of all followed users:
class PostRepository {
public function getPostsOfUser($id) {
$query = $this->getEntityManager()->createQuery(
"SELECT p.content
FROM AppBundle:Post p
WHERE p.member = :id
OR p.memberId IN (
SELECT r.friend
FROM AppBundle:Relation r
WHERE r.user = :id
)
ORDER BY p.created DESC"
);
return $query->setParameter('id', $id)
->getResult();
}
}
I have a simple spec testing the creation of an object of the Baseline class.
it "allows a user to create a baseline score with valid content" do
expect(#user.baselines.count).to eq(0)
#baseline = post(:create, :user_id => #user.id, :baseline => valid_attributes)
expect(response).to redirect_to '/patients/list'
expect(flash[:notice]).to eq("Baseline scores for case #{#baseline.case_id} was successfully created.")
expect(Baseline.all.count).to eq(1)
end
But I get this. I am uncertain where to begin with this - I am uncertain why I can't access the case_id attribute of #baseline.
NoMethodError:undefined method `case_id' for <ActionController::TestResponse:0x007f8f5ab4f3c0>
Just to show...these are the valid attributes
let(:valid_attributes) do {
:dx1 => "IPF",
:dxcon1 => 100,
:db1 => "Progressive",
:dbcon1 => 100,
:mgt=> "Drugs",
:biopsy => "Yes",
:patient_id => #patient.id,
:case_id => #patient.case,
}
end
post doesn't return a model instance it returns a TestResponse object which gives you access to headers, status code, etc. To access the object created as a side effect of calling the :create action you can do Baseline.last (in this case Baseline.first would also work since there are no existing baseline objects)
Also note - if you have an instance variable named #baseline that is assigned in the controller you can access that with assigns(:baseline)
expect(assigns[:baseline]).to be_a(Baseline)
To allow me to quickly filter records in ActiveAdmin i've defined scopes on my model. I have "shipped" and "unshipped" scopes as below. For some reason the "Shipped" scope is working as expected and shows the number of shipped items but the "Unshipped" scope doesn't do anything, it doesn't seem to know what is unshipped. It seems that i have to check and then uncheck "Shipped" checkbox in order for it to know that it's unshipped??
ORDER MODEL
class Order < ActiveRecord::Base
scope :shipped, where(:shipped => true)
scope :unshipped, where(:shipped => false)
end
ADMIN ORDER MODEL
ActiveAdmin.register Order do
scope :all, :default => true
scope :shipped
scope :unshipped
index do
selectable_column
column "Status", :sortable => :shipped do |s|
status_tag((s.shipped? ? "Shipped" : "Unshipped"), (s.shipped? ? :ok : :warning))
end
end
end
Can anyone see what the problem is?
Many Thanks
Is that the actual code from your model?
It should be:
scope :shipped, -> { where(shipped: true) }
scope :unshipped, -> { where(shipped: false) }
Realised that shipped was not by default set to false so fixed the issue by doing so in the Orders table.
I created my own CustomFieldFormat :
init.rb
Redmine::CustomFieldFormat.map do |fields|
fields.register
MyCustomFieldFormat.new('my', :label => :label_test, :order => 1 + 0.6)
end
lib/my_custom_field_format.rb
class MyCustomFieldFormat < Redmine::CustomFieldFormat
def format_value(value, field_format)
"test"
end
def show_value(custom_value)
"test"
end
end
I would like to modify the value of the field while updating. (For example, return "test" and not the value stored in the database). But nothing happens, there is always the true value in the field and not "test". Why ?
Thanks