If I take a number Object like so :
#objects = Object.all[1..5]
I no longer can perform a where method on #object.
Is there anyway, I can still perform..
#objects.where(:attribute => identity)
So long as I know all the objects are of the same class?
Once you triggered all an Array instance is returned, so answer to your question is no. There are some gotchas, though:
Keep a scope variable. I.e. if you need to use a scoped object in multiple places, do the following:
objects = Object.scoped
all_objects = objects.all
special_objects = objects.where(attribute: something_special).all
Continue playing with scoping:
objects = Object.skip(1).take(5)
all_objects = objects.all
special_objects = objects.where(attribute: something_special).all
Hacky and inefficient way:
all_objects = Object.all[1..5]
special_objects = object.select { |object| object.attribute == something_special }
Related
I want to avoid writing these three lines over and over in different functions.
var newState = state.copy();
newState.user.googleUser = googleUser;
state = newState;
Something like this?
state = (state.copy().user.googleUser = googleUser);
Use the cascade operator:
state = state.copy()..user.googleUser = googleUser;
Alternatively, a common pattern is to instead create a copyWith method for your classes so that you can do:
state = state.copyWith(user: googleUser);
According to official Dart documentation, it is mentioned that:
Chaining method calls
a..b = true..c = 5;
Cascade used for chaining access to methods and other members.
Equivalent:
a.b = true; a.c = 5;
Can someone explain the meaning of the above lines?
Consider user as an instace of following class:
class User {
int age;
String firstName;
String lastName;
}
Now you can update data in user as following:
user.age = 5;
user.firstName = 'John';
user.lastName = 'Doe';
but if you use chaining access as described in the documentation.
Then it will look something like this:
user..age = 5
..firstName = 'John'
..lastName = 'Doe';
Note: sequence of assignment of the properties does not matter, but it might be of importance while calling functions or methods like this.
Just a simple & random example:
painter..computeLayout()
..initializePaint()
..render();
above is same as:
painter.computeLayout();
painter.initializePaint();
painter.render();
in the above example all of the methods were called on painter object/instance but the sequence here must be followed as without paint or layout the render will not work.
Cascades
To perform a sequence of operations on the same object, use cascades (..). We’ve all seen an expression like this:
myObject.someMethod()
It invokes someMethod() on myObject, and the result of the expression is the return value of someMethod().
Here’s the same expression with a cascade:
myObject..someMethod()
Although it still invokes someMethod() on myObject, the result of the expression isn’t the return value — it’s a reference to myObject! Using cascades, you can chain together operations that would otherwise require separate statements. For example, consider this code:
var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));
With cascades, the code becomes much shorter, and you don’t need the button variable:
querySelector('#confirm')
..text = 'Confirm'
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
Given
Dim postviewmodel As IEnumerable(Of be_PostsViewModel)
postviewmodel = _postRepository.SelectAll.Select(Function(S) New
be_PostsViewModel With {.PostIsPublished = S.PostIsPublished, .Id =
S.PostId, .PostSummary = S.PostSummary, .PostDateCreated =
S.PostDateCreated,
.PostCategory = S.PostCategory, .PostTitle =
S.PostTitle}).Where(Function(p)
p.PostIsPublished = True).Where(Function(a) Not
a.PostCategory.FirstOrDefault.CategoryName = "Lead Story")
.Skip((Page - 1) * PageSize).Take(PageSize)
How would I use Automapper so I don't do all thmapping of properties by hand? I am completely new to it and have been doing a lot of reading but don't quite understand how to actually do it. I know I have to create the map and then call Mapper.map. But what goes where in Mapper.map?
I would do this:
Mapper.CreateMap<Post, be_postsViewModel>();
Do that in your application startup (App_Start etc). Then in your code above:
postViewModel = _postRepository.SelectAll.Project.To(Of be_PostsViewModel)
However, I'd put the "Project.To" AFTER all of your Where/Skip/Take pieces. The projection is the last thing you want to do. Project.To creates that exact Select expression, passes it to the query provider to modify the SQL at its base.
Unless SelectAll doesn't return IQueryable, then you have other, larger problems.
I'm passing find an array of ids, and i'd like to keep the objects in the same order i pass. I assume the order they get set is whatever the primary order set to in the model.
order of array
just items = [488800, 489404, 485616, 380112, 501101, 485606, 485612, 485619, 480304, 493609, 496200, 496203, 503000, 499111, 488802, 488825, 501700]
Order of what active record gives
#item_recomendations = CatalogItem.find(just_items)
#item_recomendations.map {|x| x.id }
=> [380112, 480304, 485606, 485612, 485616, 485619, 488800, 488802, 488825, 489404, 493609, 496200, 496203, 499111, 501101, 501700, 503000]
#item_recomendations = CatalogItem.find(just_items).sort_by{|x| just_items.index x.catalog_item_id }
If performance isn't an issue (and you should try this and benchmark first to prove it is), just iterate over the array yourself:
items.collect { |id| CatalogItem.find(id) }
Example java code is below if I have a class Movie
In java I will create its array by writing below code
Movie[] a = new Movie[4];
but how I can do it in rails So that when I check it on the console
#> a.type
#> "Movie"
Reather than
#> ActiveRecord::Relation
You just create a class by inherited from Array like below,
class MyArray < Array
#Add you custom methods
end
my_array = MyArray.new([1,2,3,4,5]) or
my_array = MyArray.new
my_array[0] = 1
my_array[1] = 2
That enough for basic customization of array data structure.
Literal translation would be
a = (1..4).map { Movie.new }
or (in Rails)
a = (1..4).map { Movie.create! }
but you normally shouldn't need to do it, since unlike in Java, you don't have limited-size arrays in Ruby.
Also, the type of that would then be Array (or rather its class; basic Ruby objects don't have type); the type of an element of that, a[0] for example, would be a Movie.
In the end, not quite sure what you're asking here...